关于Android软键盘在全屏下设置adjustResize无效的问题

Android软键盘在全屏下设置adjustResize无效的问题。
这时我们可以在activity的根布局上添加fitsSystemWindows="true",然后adjustResize属性就会起作用。但是这样做还有一个问题,我们的界面会下移一段距离,这段距离的高度相当于状态栏的高度。

public class WindowInsetsLinearLayout extends LinearLayout {

    private static final String TAG = WindowInsetsLinearLayout.class.getSimpleName();

    public WindowInsetsLinearLayout(@NonNull Context context) {
        this(context, null);
    }

    public WindowInsetsLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindowInsetsLinearLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private int[] mInsets = new int[4];
    @Override
    protected final boolean fitSystemWindows(Rect insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            mInsets[0] = insets.left;
            mInsets[1] = insets.top;
            mInsets[2] = insets.right;
            insets.left = 0;
            insets.top = 0;
            insets.right = 0;
        }
        return super.fitSystemWindows(insets);
    }

    @Override
    public final WindowInsets onApplyWindowInsets(WindowInsets insets) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT_WATCH) {
            mInsets[0] = insets.getSystemWindowInsetLeft();
            Log.e("mInsets[0]", "" + mInsets[0]);
            mInsets[1] = insets.getSystemWindowInsetTop();
            Log.e("mInsets[1]", "" + mInsets[1]);
            mInsets[2] = insets.getSystemWindowInsetRight();
            Log.e("mInsets[2]", "" + mInsets[2]);
            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,
                    insets.getSystemWindowInsetBottom()));
        } else {
            return insets;
        }
    }
}

使用

    

        

        
    

你可能感兴趣的:(关于Android软键盘在全屏下设置adjustResize无效的问题)