android怎么设置状态栏和actionbar同颜色?
关键代码:
BaseActivity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//这就是透明
}
//透明之后所有的头部都会顶上来,然后就是加paddingtop了
//头部layout公用,rl_title_content是最外层的RelativeLayout。
RelativeLayout rl_title_content = findViewById(R.id.rl_title_content);
if (rl_title_content != null) {
//getStatusBarHeight是获取状态栏的高度的
rl_title_content.setPadding(0,Utils.getStatusBarHeight(this),0,0);//左上右下
}
public static int getStatusBarHeight(Context context) {
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
sbar = context.getResources().getDimensionPixelSize(x);
} catch (Exception e1) {
e1.printStackTrace();
}
return sbar;
}