安卓设置沉浸式状态栏

Posted lucktian

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了安卓设置沉浸式状态栏相关的知识,希望对你有一定的参考价值。

 

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();
}
}



















































以上是关于安卓设置沉浸式状态栏的主要内容,如果未能解决你的问题,请参考以下文章

安卓设置沉浸式状态栏

安卓5.0 沉浸式状态栏

Android 沉浸式状态栏

安卓沉浸式状态栏

Android 4.4 沉浸式透明状态栏与导航栏

安卓中实现状态栏沉浸式效果