安卓软键盘弹出时导致布局错乱

15

这是一个布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1" />

        <FrameLayout
            android:background="#0000ff"
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1" />

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0px"
            android:layout_weight="1" />

    </LinearLayout>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true" />

</RelativeLayout>

enter image description here

当EditText被触摸并出现软键盘时,我希望蓝色条保持不变。也就是说,我希望它的大小和位置得到保留。我尝试了AndroidManifest中我的activity参数android:windowSoftInputMode的四个可能值。然而每次当软键盘出现时,我的蓝色条都会移动或缩小:

android:windowSoftInputMode="adjustUnspecified" 或默认值:

enter image description here

android:windowSoftInputMode="adjustPan"android:windowSoftInputMode="adjustNothing"

enter image description here

android:windowSoftInputMode="adjustResize"

enter image description here

是否有可能在软键盘出现时保持其相同的大小和位置?


5
我尝试使用 adjustNothing,在我的设备上(Google Nexus)似乎有效。 - sdabet
3个回答

6

您必须使用<ScrollView>作为主要布局。

示例:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">
   ....
</ScrollView>

或者在 AndroidManiFest.xml 中使用以下代码:

<activity
     android:windowSoftInputMode="adjustNothing">
</activity>

1

由于输入是进入您的EditText,它总是会移动某些内容。

考虑拥有两个布局,一个用于键盘关闭时,另一个用于键盘打开时。

使用ViewSwitcher可能会对此有所帮助。


0
我认为这是由于您使用的LinearLayout引起的。您能否将内部布局更改为RelativeLayout?在您目前的情况下,键盘只会因为线性布局而更改组件之间的边距。并且请将您的配置更改为adjustNothing。

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