您的位置:首页 > 移动开发 > Android开发

Android头部状态栏,menu背景自定义

2015-11-13 09:15 459 查看
1.首先去参考下:https://github.com/jgilfelt/SystemBarTint

下载相关jar包放置项目中的lib目录

2.在BaseActivity中重写Oncreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStatusTint();
}
//设置状态栏的颜色
private void setStatusTint() {

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

SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(R.color.bg_btn);//通知栏所需颜色

}

@TargetApi(19)
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);

}


3.这个时候你会遇到一个问题



对!你的toolbar或者actionbar或者none actionbar的都往上顶了,

解决方法:重写setContentView,这样所有的view都设置了

public void setContentView(View view, LayoutParams params) {
//**该方法需要在setContentView方法之后调用。
重写BaseActivity中的setContentView(int layoutResID )方法,就可以实现全局地设置所有的Activity界面的状态栏颜色,如果项目中某个Activity需要设置不同的状态栏颜色,显式地调用该方法,并传入颜色值即可**

((ViewGroup)findViewById(android.R.id.content)).getChildAt(0).setFitsSystemWindows(true);
}


也可以这样单独设置



简单的讲:设置应用布局时是否考虑系统窗口布局;如果为true,将调整系统窗口布局以适应你自定义的布局。比如系统有状态栏,应用也有状态栏时。看你这个布局代码,恰恰是在定义标题栏样式,所以用到这行代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: