Android点击EditText软键盘遮挡按钮 adjustResize不生效解决方法
问题根源
你的adjustResize配置不生效核心是两个问题:
- 根布局高度配置错误:你写的根
LinearLayout高度是wrap_content,adjustResize的逻辑是软键盘弹出时压缩Activity的可用窗口高度,只有根布局高度设为match_parent时,布局才会跟随窗口高度重排,把底部控件往上推;根布局用包裹内容的话,窗口高度变化不会触发它的位置调整,底部按钮自然会被软键盘挡住。 - 边到边显示冲突:如果你在代码里开了沉浸式边到边显示(比如调用过
WindowCompat.setDecorFitsSystemWindows(window, false),或是使用了默认开启边到边的新版Material主题),系统不会自动帮你处理软键盘的窗口缩进,Manifest里的adjustResize配置会直接失效。
额外提一句:你在Toolbar控件上写的所有app:layout_constraintXXX属性都是无效的,这些属性只有父布局是ConstraintLayout时才会生效,你的父布局是LinearLayout,可以直接删掉这些冗余参数。
修复方案
第一步:修改布局文件
- 把根
LinearLayout的android:layout_height="wrap_content"改成android:layout_height="match_parent",让布局占满整个窗口可用高度。 - 给笔记内容对应的
TextInputLayout(id是addnote_descp的那个)加上android:layout_weight="1",同时把它的android:layout_height改成match_parent,让中间输入区域自动占满Toolbar和底部按钮之间的剩余空间,保证底部按钮始终贴在布局最底部。
改完的核心布局片段参考:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/addnote" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/my_toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextColor="@color/white" /> <com.google.android.material.textfield.TextInputLayout android:id="@+id/addnote_title" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="5dp" android:layout_marginTop="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:hint="Title"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/addnote_title_text" android:layout_width="match_parent" android:layout_height="wrap_content" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/addnote_descp" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="5dp" android:layout_marginRight="5dp" android:layout_marginBottom="10dp" android:layout_weight="1" android:hint="Notes"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/addnote_descp_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="top" android:lines="10" /> </com.google.android.material.textfield.TextInputLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="2dp"> <ImageButton android:id="@+id/check_btn" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="@color/black" android:layout_marginRight="2dp" android:src="@drawable/checkbox"/> <ImageButton android:id="@+id/bold_btn" android:layout_width="0dp" android:layout_height="50dp" android:layout_weight="1" android:background="@color/black" android:layout_marginLeft="2dp" android:src="@drawable/format_bold"/> </LinearLayout> </LinearLayout>
改完之后如果没开边到边显示,软键盘弹出时底部按钮会自动被顶到软键盘上方,不需要额外改代码。
第二步:适配边到边显示场景(改完布局还是不生效就做这步)
如果你开了边到边显示,直接在Activity的onCreate方法里加下面的代码,手动监听软键盘高度给布局加底部边距,这个方案兼容所有Android版本,不会出现适配失效问题:
val rootLayout = findViewById<LinearLayout>(R.id.addnote) ViewCompat.setOnApplyWindowInsetsListener(rootLayout) { view, windowInsets -> val imeHeight = windowInsets.getInsets(WindowInsetsCompat.Type.ime()).bottom view.setPadding(view.paddingLeft, view.paddingTop, view.paddingRight, imeHeight) WindowInsetsCompat.CONSUMED }
你Manifest里的android:windowSoftInputMode="adjustResize"配置不需要改,保持原样就行。
内容的提问来源于stack exchange,提问作者Abhilash Das
相关产品推荐
相关产品推荐

