Android布局:当输入法弹出时,按钮会移动

3
当键盘弹出时,我的布局会发生改变。特别是注册按钮会向上移动并停留在一个EditText之上。我希望在键盘打开时保持视图大小不变,并能在较小的视口中滚动它。我尝试了android:windowSoftInputMode="adjustResize"(按钮被移动)和"adjustPan"(按钮不会被移动,但要看到按钮,您首先必须关闭键盘)。以下是我的xml文件:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#ffffff">
    <LinearLayout 
        android:id="@+id/register_header"
        android:orientation="horizontal"
        android:gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:padding="@dimen/activity_vertical_margin">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ... />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
           ... />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/register_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/register_header"
        android:padding="@dimen/activity_vertical_margin"
        android:orientation="vertical" >

        // Bunch of EditText's here


    </LinearLayout>
    <LinearLayout
        android:id="@+id/register_footer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:gravity="center_vertical|bottom"
        android:orientation="vertical"
        android:padding="@dimen/activity_vertical_margin">

        <Button 
            android:padding="16dp"
            android:id="@+id/register_button_register"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/register" />

    </LinearLayout>

</RelativeLayout>

2个回答

3

请将以下内容放在"the" activity标签下的manifest文件中

 android:windowSoftInputMode="adjustNothing"

了解更多信息,请访问键盘输入


adjustNothing不是Android API中的属性,我也读过一些帖子在其中使用它,但由于不同设备及其不同的处理方式,这可能是危险的。 - Opiatefuchs
不幸的是,尽管这解决了按钮移动的问题,但当键盘可见时,它也不允许我滚动内容。在这方面,它的行为类似于adjustPan - Cuddl3s

1

不要使用RelativeLayout作为ScrollView的子项,而是像这样使用LinearLayout

<ScrollView>
     <LinearLayout orientation="vertical">
       <LinearLayout orientation="vertical">
         <EditTexts />
       </LinearLayout>
       <Button />
    </LinearLayout>
<ScrollView>

当您拥有一个 RelativeLayout 和一个具有 alignParentBottom=true 的子元素时,该子元素将始终位于屏幕底部,在 adjustResize 情况下意味着在键盘上方和 EditTexts 上方。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接