Android透明状态栏(4.4以上版本)

来源:互联网 发布:陈赫品牌淘宝店 编辑:程序博客网 时间:2024/05/16 13:40

关键代码:

protected void initSystemBar() {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            Window window = getWindow();            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);            window.setStatusBarColor(Color.TRANSPARENT);            window.setNavigationBarColor(Color.TRANSPARENT);        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            Window window = getWindow();            // Translucent status bar            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);            // Translucent navigation bar            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);        }    }

问题:
1.状态栏透明之后,布局会统一上移,所以需要在第一个布局上面加paddingTop,一般导航栏写成一个控件TopBar,然后每个页面去引用, 可以建values-v19 和values-21文件夹,在dimens.xml里面去配置TopBar的高度和paddingTop
2.透明状态栏会与底部输入框有冲突,导致adjustResize不起作用,输入框不能被软键盘弹起来,解决方法是重写activity的根布局,然后在根布局上设置fitSystemWindows=”true”,这个忘了设置是不行的

/** * Created by star on 2017/1/20 * 功能: 解决透明状态栏和底部输入框的冲突问题,将activity的根布局替换成该类,需要什么布局就继承哪个ViewGroup,比如需要RelativeLayout就extends RelativeLayout */public class CustomInsetsLinearLayout extends LinearLayout {    private int[] mInsets = new int[4];    public CustomInsetsLinearLayout(Context context) {        super(context);    }    public CustomInsetsLinearLayout(Context context, AttributeSet attrs) {        super(context, attrs);    }    public CustomInsetsLinearLayout(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public final int[] getInsets() {        return mInsets;    }    @Override    protected final boolean fitSystemWindows(Rect insets) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            // Intentionally do not modify the bottom inset. For some reason,            // if the bottom inset is modified, window resizing stops working.            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();            mInsets[1] = insets.getSystemWindowInsetTop();            mInsets[2] = insets.getSystemWindowInsetRight();            return super.onApplyWindowInsets(insets.replaceSystemWindowInsets(0, 0, 0,                    insets.getSystemWindowInsetBottom()));        } else {            return insets;        }    }}
  1. fitSystemWindows=”true”意思是布局是否从状态栏下面开始排列
  2. clipToPadding默认为true,如果最上面布局有paddingTop,例如ListView,则表示滑动的时候paddingTop能否被滑走,true为不滑走,false为滑走
0 0