解决Android 4.4沉浸式状态栏一些小问题

看了别人的App,状态栏和标题栏一个颜色,感觉好看极了,于是研究了Android状态栏,因为自己手机是Android4.4,于是就查了一些资料。
就简单的几步就可以完成,首先在代码里实现:

写在onCreate里

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            setTranslucentStatus(true);
        }

        SystemBarTintManager tintManager = new SystemBarTintManager(this);
        tintManager.setStatusBarTintEnabled(true);
        tintManager.setNavigationBarTintEnabled(true);

        // 自定义颜色
        tintManager.setTintColor(Color.parseColor("#508aeb"));

以及用到的方法

private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

这里要用到github上的开源项目SystemBarTint ,因为不用这个开源项目,你实现了这句

WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;

使得状态栏透明,而且标题栏会跑到状态栏上去,然后你设置

<item name="android:fitsSystemWindows">trueitem>

之后虽然状态栏和标题栏位置固定好了,但是状态栏颜色变透明了。之后解决的办法就是参照开篇的代码,利用这个开源项目,导入module依赖就搞定了。

你可能感兴趣的:(新知识的学习)