沉浸式状态栏使用记录

//状态栏类型
public static final int STATUSBAR_TYPE_TRANSPARENT_WHITE = 1; //状态栏类型为透明底部,白色字体图标
public static final int STATUSBAR_TYPE_WHITE_BLACK = 2; //状态栏类型为白色底部,黑色字体图标
public static void setStatusBar(Activity pActivity, int pStatusbarType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//5.x开始需要把颜色设置透明,否则导航栏会呈现系统默认的浅灰色
Window window = pActivity.getWindow();
View decorView = window.getDecorView();
//两个 flag 要结合使用,表示让应用的主体内容占用系统状态栏的空间
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
if (pStatusbarType == STATUSBAR_TYPE_WHITE_BLACK) {
option |= View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR;
}
decorView.setSystemUiVisibility(option);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

if (pStatusbarType == STATUSBAR_TYPE_WHITE_BLACK) {
//背景
window.setStatusBarColor(Color.WHITE);
} else if (pStatusbarType == STATUSBAR_TYPE_TRANSPARENT_WHITE) {
//背景
window.setStatusBarColor(Color.TRANSPARENT);
}
//底部导航栏颜色
// window.setNavigationBarColor(Color.TRANSPARENT);
// window.setNavigationBarColor(ContextCompat.getColor(activity, R.color.black));
} else {
//以下这种情况没有测试过
/* if (pStatusbarType == UtilsKey.STATUSBAR_TYPE_WHITE_BLACK) {
setStatusBar2(pActivity);
} else if (pStatusbarType == UtilsKey.STATUSBAR_TYPE_TRANSPARENT_WHITE) {*/
Window window = pActivity.getWindow();
WindowManager.LayoutParams attributes = window.getAttributes();
int flagTranslucentStatus = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
attributes.flags |= flagTranslucentStatus;
/*int flagTranslucentNavigation = WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION;
attributes.flags |= flagTranslucentNavigation;*/
window.setAttributes(attributes);
// }
}
}
}

public static void setStatusBar2(Activity pActivity) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
pActivity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
pActivity.getWindow().setStatusBarColor(Color.WHITE);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
pActivity.getWindow().setStatusBarColor(ContextCompat.getColor(pActivity, R.color.translucent));
}
pActivity.getWindow().setNavigationBarColor(ContextCompat.getColor(pActivity, R.color.black));
}
}
posted @ 2017-12-19 15:52  宋者为王  阅读(416)  评论(0编辑  收藏  举报