Android输入法遮挡了输入框,使用android:fitsSystemWindows="true"后界面顶部出现白条

本文介绍了一种解决Android应用中输入法遮挡输入框及界面顶部出现状态栏白条的方法,通过自定义LinearLayout并重写fitSystemWindows和onApplyWindowInsets方法,实现了软键盘弹出时的界面适配。
部署运行你感兴趣的模型镜像

问题: 

1、页面布局文件:

<com.zhy.autolayout.AutoLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_order_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

2、配置文件不设置android:windowSoftInputMode属性;

 

效果图:

 

3、加入android:fitsSystemWindows="true"后,解决了输入法遮挡了输入框的问题,但是界面顶部出现了状态栏高度的白条。

 

解决方法:

1、自定义CustomLinearLayout(因为我页面最外层是LinearLayout)继承LinearLayout,重写fitSystemWindows和onApplyWindowInsets两个方法:

public class CustomLinearLayout extends AutoLinearLayout {
    public CustomLinearLayout(Context context) {
        super(context);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            insets.left = 0;
            insets.top = 0;
            insets.right = 0;
        }
        return super.fitSystemWindows(insets);
    }

    @RequiresApi(api = Build.VERSION_CODES.KITKAT_WATCH)
    @Override
    public WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0, insets.getSystemWindowInsetBottom()));
        } else {
            return insets;
        }
    }
}

2、修改布局文件:

<com.example.widget.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout_order_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

3、配置文件不设置android:windowSoftInputMode属性;

4、效果图:

问题解决。

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

所给参考引用内容未涉及当 `android:fitsSystemWindows=true` 时使用输入法导致布局被拉长问题的解决方案。不过,通常可采用以下几种常见解决办法: ### 设置 `adjustResize` 在 `AndroidManifest.xml` 中对应 `Activity` 的配置里,将 `windowSoftInputMode` 设置为 `adjustResize`。示例如下: ```xml <activity android:name=".YourActivity" android:windowSoftInputMode="adjustResize|stateHidden"> </activity> ``` 这样设置后,当输入法弹出时,布局会自动调整大小以适应输入法的空间,从而避免布局被拉长。 ### 自定义根布局 可以自定义一个根布局,重写 `onMeasure` 或 `onSizeChanged` 方法,在输入法弹出时手动控制布局的尺寸。示例代码如下: ```java import android.content.Context; import android.util.AttributeSet; import android.widget.LinearLayout; public class CustomLinearLayout extends LinearLayout { public CustomLinearLayout(Context context) { super(context); } public CustomLinearLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); // 在这里处理布局尺寸变化的逻辑 } } ``` 然后在布局文件中使用自定义的根布局: ```xml <com.example.yourpackage.CustomLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 其他子视图 --> </com.example.yourpackage.CustomLinearLayout> ``` ### 使用 `ViewTreeObserver` 通过 `ViewTreeObserver` 监听布局的变化,当输入法弹出时进行相应的处理。示例代码如下: ```java final View rootView = findViewById(android.R.id.content); rootView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { int heightDiff = rootView.getRootView().getHeight() - rootView.getHeight(); if (heightDiff > 100) { // 可能是输入法弹出 // 处理布局调整逻辑 } else { // 输入法隐藏,恢复布局 } } }); ```
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值