Android:当键盘出现时使布局可滚动

cap*_*com 2 android android-layout

这是对这个问题的跟进.

在这个问题上花了好几天,似乎无法得到我想要的东西.我需要的是,在触摸edittext字段后弹出键盘时,整个视图可以滚动:

在此输入图像描述

请注意,此视图不可滚动,不需要.

因此,当用户点击edittext字段(比如第一个)时,它看起来像这样:

在此输入图像描述

如果他/她选择,视图可以滚动到不再以它在这里看起来的方式结束:

在此输入图像描述

我已经尝试过我能想到的每个scrollview组合,但它所做的就是拉伸我的背景图像.我确信有一个简单的解决方案,但我找不到它.我真的很感激能解决这个问题的人.以下是布局的XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/background"
    android:orientation="vertical"
    android:paddingLeft="60dp"
    android:paddingTop="53dp" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="173dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView1"
        android:layout_marginTop="3dp"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText1"
        android:layout_marginTop="3dp"
        android:text="text2"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="173dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView2"
        android:layout_marginTop="3dp"
        android:inputType="numberDecimal" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText2"
        android:layout_marginTop="3dp"
        android:text="text3"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/editText3"
        android:layout_width="173dp"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView3"
        android:layout_marginTop="3dp"
        android:inputType="numberDecimal" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/editText3"
        android:layout_marginTop="3dp"
        android:text="text4" />

    <ImageView
        android:id="@+id/gearImage3"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@id/button1"
        android:layout_marginTop="70dp"
        android:clickable="true"
        android:contentDescription="@string/gearDescription_string"
        android:src="@drawable/gear" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

请注意,后两个图像不是来自真实的应用程序(photoshopped显示我需要的),但第一个是来自我的应用程序.谢谢.

edw*_*ard 5

ScrollView仅滚动到有数据的位置.因此,只需添加ScrollView就可以解决您的问题.将其添加到xml的开头:

 <ScrollView
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/ScrollView"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

然后显然在其他所有内容之后将其添加到底部:

 </ScrollView>
Run Code Online (Sandbox Code Playgroud)

我在我的应用程序中使用它,当键盘显示时,我仍然可以滚动到页面底部.如果屏幕截图1中显示的字段不超过一个普通电话页面,则无论如何都不会滚动.它只会在键盘打开时滚动,因为它无法看到整个屏幕.

希望这可以帮助!