这是我的第一遍文章
主要用来记录我在开发中遇到的问题或者是注意事项
这次是状态栏沉浸式效果
状态栏沉浸式效果在Android4.4以后才出现的
说下我所理解的沉浸式效果
所谓的沉浸式就是透明导航栏和状态栏
然后再把状态栏的颜色颜色修改为自己想要的颜色(一般是与标题栏),所以我们必须知道要把状态栏改为什么颜色
Android4.4就是sdk19
所以我们做个函数
@TargetApi(19)
private void initWindow(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
}
这个函数在onCreate()中调用
这时我们会发现布局被拉伸了,状态栏没有了,这显然不是我们想要的结果
这时我想在主布局中加个id
然后使用
layout_top.setPadding(0,getResources().getDimensionPixelSize(resourceId), 0, 0);将状态栏撑开
最后就是给状态栏上色
这边我们引入github以为大神的systembartint-1.0.4.jar 稍后提供jar包和github地址
然后在我们的initWindow方法中添加
SystemBarTintManager barTintManager=new SystemBarTintManager(this);
barTintManager.setStatusBarTintEnabled(true);
barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));
即
@TargetApi(19)
private void initWindow(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
layout_top.setPadding(0,getResources().getDimensionPixelSize(resourceId), 0, 0);
SystemBarTintManager barTintManager=new SystemBarTintManager(this);
barTintManager.setStatusBarTintEnabled(true);
barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));
}
这样就完成了我们的沉浸式状态栏
是不是觉得一定一个ID然后再用findViewById(),有点麻烦?
我从网上找到解决方法在布局文件root中添加
android:fitsSystemWindows="true"
这么一句话就可以省掉麻烦了
@TargetApi(19)
private void initWindow(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
SystemBarTintManager barTintManager=new SystemBarTintManager(this);
barTintManager.setStatusBarTintEnabled(true);
barTintManager.setStatusBarTintColor(this.getResources().getColor(R.color.xxxxxxxxxxxx));
}
下面是github的地址
https://github.com/jgilfelt/SystemBarTint
布吉岛博客怎么传jar包