安卓设置沉浸式状态栏

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
| WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
window.setNavigationBarColor(Color.TRANSPARENT);
}
setTitleBarColor(status_bar, R.color.new_login_colors);

/**
* @params status_bar 顶替状态栏的view
* @params color 状态栏颜色
*/
public void setTitleBarColor(View status_bar, int color) {
try {
// 设置View的高度,因为每个型号的手机状态栏高度都不相同
status_bar.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, ScreenUtils.getStatusHeight(this)));
// 判断SDK版本是否大于等于19,大于就让他显示,小于就要隐藏,不然低版本会多出来一个
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
setTranslucentStatus(true);
status_bar.setVisibility(View.VISIBLE);
} else {
status_bar.setVisibility(View.GONE);
}
// 为状态栏着色
SystemBarTintManager tintManager = new SystemBarTintManager(this);
tintManager.setStatusBarTintEnabled(true);
tintManager.setStatusBarTintResource(color);
} catch (Exception e) {
e.printStackTrace();
}
}

@TargetApi(19)
private void setTranslucentStatus(boolean on) {
try {
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);
} catch (Exception e) {
e.printStackTrace();
}
}
原文地址:https://www.cnblogs.com/lucktian/p/6951600.html