1. 程式人生 > >安卓設置沈浸式狀態欄

安卓設置沈浸式狀態欄

system its code itl lollipop one res titlebar nds

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

安卓設置沈浸式狀態欄