先看下通用的代碼
// 5.0以上系統(tǒng)狀態(tài)欄透明
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
int statusBarHeight1 = -1;
//獲取status_bar_height資源的ID
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
//根據(jù)資源ID獲取響應(yīng)的尺寸值
statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
}
//設(shè)置contentview為fitsSystemWindows
ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
View childAt = contentView.getChildAt(0);
if (childAt != null) {
childAt.setFitsSystemWindows(true);
}
//給statusbar著色
View view = new View(this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight1));
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
contentView.addView(view);
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
int statusBarHeight1 = -1;
//獲取status_bar_height資源的ID
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
//根據(jù)資源ID獲取響應(yīng)的尺寸值
statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);
}
//設(shè)置contentview為fitsSystemWindows
ViewGroup contentView = (ViewGroup) findViewById(android.R.id.content);
View childAt = contentView.getChildAt(0);
if (childAt != null) {
childAt.setFitsSystemWindows(true);
}
//給statusbar著色
View view = new View(this);
view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,statusBarHeight1));
view.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
contentView.addView(view);
}
對(duì)于5.0以上的系統(tǒng),狀態(tài)欄可以直接調(diào)用API去實(shí)現(xiàn)。
getWindow().setStatusBarColor(getResources().getColor(R.color.trans));
對(duì)于6.0以上的系統(tǒng),部分機(jī)型可以直接調(diào)用API去改變狀態(tài)欄圖標(biāo)的顏色。