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

来源:互联网 发布:mac 系统使用 编辑:程序博客网 时间:2024/04/29 00:33

看了别人的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">true</item>

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

0 0
原创粉丝点击