You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android特定设备adjustResize模式关闭软键盘后残留空白问题求助

这个问题是部分Android定制ROM的系统层级异常:软键盘隐藏时没有正确触发窗口尺寸重新测量分发,结合ScrollView的fillViewport="true"属性会保留上一次测量高度,就会出现底部等高空白。可按以下优先级尝试解决方案:

方案1:监听软键盘状态主动触发重绘(兼容性最好)

在对应Activity的onCreate方法中添加如下代码,监听软键盘隐藏事件,强制根布局重新测量绘制:

// 获取根布局视图
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
rootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
    Rect r = new Rect();
    rootView.getWindowVisibleDisplayFrame(r);
    int screenHeight = rootView.getRootView().getHeight();
    int heightDiff = screenHeight - (r.bottom - r.top);
    // 软键盘高度通常大于200dp,小于该值说明软键盘已隐藏
    if (heightDiff < dp2px(this, 200)) {
        rootView.requestLayout();
    }
});

// dp转px工具方法
private int dp2px(Context context, float dpValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dpValue * scale + 0.5f);
}

Kotlin实现逻辑完全一致,按语法调整即可。

方案2:调整fitsSystemWindows配置位置

你之前给根布局设置fitsSystemWindows="true"无效,可以调整配置:

  • 根布局RelativeLayout设置android:fitsSystemWindows="false"
  • 子层ScrollView设置android:fitsSystemWindows="true"
    该方案适配了部分定制ROM对WindowInsets分发异常的问题,配合fillViewport="true"可正常响应窗口尺寸变化。

方案3:替换根布局类型

如果上述两种方案都无效,可以把根布局的RelativeLayout替换为ConstraintLayout。RelativeLayout在部分低版本系统和定制ROM上的测量逻辑存在漏洞,替换后可解决大部分测量异常问题。


内容的提问来源于stack exchange,提问作者testivanivan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 17:39:03