【问题标题】:Android adjustResize layout resize with soft keyboardAndroid adjustResize 使用软键盘调整布局大小
【发布时间】:2025-12-25 10:15:10
【问题描述】:

我正在努力获得一个小的 EditText 以及与屏幕底部对齐的两个按钮软件键盘的底部。基本上我的问题是每次我打开我的活动(标记为 adjustResize | stateAlwaysVisible)和布局:

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

    <LinearLayout android:orientation="vertical" android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/soft_input_edit_placeholder">
        <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/editText1">
            <requestFocus></requestFocus>
        </EditText>
    </LinearLayout>
    <LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:weightSum="2" android:id="@+id/soft_input_layout_buttons">
        <Button android:layout_width="0dp" android:text="xxx Submit xxx" android:layout_height="wrap_content" android:id="@+id/soft_input_button_submit" android:layout_weight="1"></Button>
        <Button android:layout_width="0dp" android:text="xxx Cancel xxx" android:layout_height="wrap_content" android:id="@+id/soft_input_button_cancel" android:layout_weight="1"></Button>
    </LinearLayout>
</LinearLayout>

键盘向上滑动,它调整活动的大小,以便只有编辑文本可见,当我隐藏它时(至少键盘有一个按钮可以让我隐藏它),布局保持在原位,也就是说,它永远不会向下滑动,留下一半的屏幕未被覆盖。

我读到,在大多数情况下,对齐是通过在布局中引入一些可扩展的控件来实现的,但我真的不希望在那里有任何形式的 ListView;我尝试使用RelativeLayout,仍然不行。将一个嵌入另一个 - 仍然没有运气。非常感谢您提出的任何建议,我的想法已经不多了..

谢谢!

【问题讨论】:

    标签: android layout android-softkeyboard


    【解决方案1】:

    问题都在选定的主题中。我的活动在清单中应用了全屏主题(我不知道它可能会以这种方式影响视图);将其更改为非全屏已经解决了我的问题(虽然它不能只是 任何其他主题)

    我通过将活动主题设置为 Theme.Translucent 并将布局主题设置为 Theme(根本没有主题)来实现我的目标;将 @android:drawable/bottom_bar 应用到主 LinearLayout 并最终将主布局的重力设置为 bottom 给了我想要的结果。

    【讨论】: