fitsSystemWindows 与 paddingTop

现象:某个 ViewGroup 中装了个 EditText,不管怎么设置,ViewGroup 都出现paddingTop

原因:如果某个View 的 fitsSystemWindows 设为true,那么该View的padding属性将由系统设置,用户在布局文件中设置的

padding会被忽略。系统会为该View设置一个paddingTop,值为statusbar的高度。

解决办法:重写该 ViewGroup 的以下两个方法

    /*
        重写 fitSystemWindows 和 onApplyWindowInsets 阻止未设置的paddingTop的出现
    */
    @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;
        }
    }

 

你可能感兴趣的:(笔记,android)