键盘弹出时上移FrameLayout的问题求助
解决ConstraintLayout底部FrameLayout被软键盘遮挡的问题
先排查基础配置
- 确认
android:windowSoftInputMode="adjustResize"是配置在AndroidManifest.xml的对应Activity标签里,别在布局文件里瞎设置:
<activity android:name=".你的Activity类名" android:windowSoftInputMode="adjustResize"> </activity>
- 把根ConstraintLayout的
android:fitsSystemWindows设为false,避免系统窗口调整干扰布局高度。
调整布局约束(最关键一步)
你的FrameLayout已经绑定了父布局底部,但如果没有给可滚动内容做约束,adjustResize就起不了作用。给EditText区域套个ScrollView,让内容能滚动的同时,底部FrameLayout自动顶到键盘上方:
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 可滚动的内容区 --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@+id/bottom_frame"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <!-- 这里放你的所有EditText --> <EditText android:hint="输入框1" ... /> <EditText android:hint="输入框2" ... /> </LinearLayout> </ScrollView> <!-- 你的底部FrameLayout --> <FrameLayout android:id="@+id/bottom_frame" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/white" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"> <com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="start" android:text="Cancel" /> <com.google.android.material.button.MaterialButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="end" android:text="Continue" /> </FrameLayout> </androidx.constraintlayout.widget.ConstraintLayout>
ScrollView用0dp(match_constraint)绑定父布局顶部和FrameLayout顶部,键盘弹出时ScrollView会自动缩小,FrameLayout自然留在键盘上方。
处理全屏模式下的特殊情况
如果你的Activity是全屏主题(比如隐藏状态栏/导航栏),adjustResize会失效,要在Activity的onCreate里加这段代码:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // 强制开启adjustResize getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); // Android 11+ 额外配置 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { getWindow().setDecorFitsSystemWindows(false); } }
终极方案:手动监听键盘高度
要是以上方法都没用,就直接监听键盘高度变化,手动挪FrameLayout:
// 在Activity里加这段监听代码 View decorView = getWindow().getDecorView(); decorView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { Rect rect = new Rect(); decorView.getWindowVisibleDisplayFrame(rect); int screenHeight = decorView.getRootView().getHeight(); int keyboardHeight = screenHeight - rect.bottom; FrameLayout bottomFrame = findViewById(R.id.bottom_frame); ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) bottomFrame.getLayoutParams(); params.bottomMargin = keyboardHeight > 0 ? keyboardHeight : 0; bottomFrame.setLayoutParams(params); } });
内容的提问来源于stack exchange,提问作者KyluAce
相关产品推荐
相关产品推荐

