案例:
效果:
第一種
思路:
- android5.0以上
- 利用window將聲明Status可繪制,相關(guān)Param屬性為
FLAG_DRAW_SYSTEM_BAR_BACKGROUDS
- 調(diào)用window.setStatusBarCorlor()將Status顏色設(shè)置成actionBar顏色
這種方法有一定的局限性,滿足toolbar為單一顏色的情況蒲牧,當(dāng)toolbar顏色背景為漸變色/圖片時(shí)無法適用躺酒。
代碼實(shí)現(xiàn):
// activity中
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
// 顏色為狀態(tài)欄顏色啸箫,可在style中查看
getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimary));
}
- 5.0以下
第二種
通常配合Toolbar使用馆里,關(guān)于隱藏ActionBar添加Toolbar的方法不是本篇重點(diǎn)轩勘,這里不進(jìn)行贅述筒扒。
思路:將status設(shè)為半透明,在系統(tǒng)布局中加上一個(gè)view充當(dāng)status的背景
// activity中
private View mStatusView;
private void addToolbarTransparent() {
// 為window設(shè)立半透明狀態(tài)欄的標(biāo)簽
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (mStatusView == null) {
mStatusView = new View(this);
// 屏幕寬度
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getStatusBarHeight();
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(screenWidth,screenHeight);
mStatusView.setLayoutParams(layoutParams);
mStatusView.requestLayout();
// 獲取系統(tǒng)布局
ViewGroup systemContent = findViewById(android.R.id.content);
// 系統(tǒng)布局的第一個(gè)元素(用戶布局)為 status 預(yù)留空間绊寻,文章末尾對該方法進(jìn)行補(bǔ)充
systemContent.getChildAt(0).setFitsSystemWindows(true);
systemContent.addView(mStatusView, 0);
// 設(shè)置背景顏色花墩,這里也可設(shè)置drawable背景
mStatusView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
}
/** 通過Resource獲取status高度 **/
private int getStatusBarHeight() {
int statusBarHeight = -1;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) {
statusBarHeight = getResources().getDimensionPixelSize(resourceId);
}
return statusBarHeight;
}
該方法設(shè)置透明狀態(tài)欄的好處是可以任意處理我們添加的StatusView,比如一個(gè)activity中多個(gè)fragment切換時(shí)toolbar顏色跟著切換澄步,status設(shè)置為圖片背景冰蘑、漸變色等。
但該方法也有一定局限性村缸,由于我們給window設(shè)定的標(biāo)簽
FLAG_TRANSLUCENT_STATUS
是將狀態(tài)欄設(shè)置為半透明祠肥,可讓低版本的status全透明,但高版本機(jī)型會出現(xiàn)以下情況這時(shí)我們需要利用DecorView的兩個(gè)標(biāo)簽
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
和SYSTEM_UI_FLAG_LAYOUT_STABLE
梯皿,并結(jié)合DecorView#setStatusColor()(5.0以上)將狀態(tài)欄設(shè)置為完全透明仇箱,并將FitsSystemWindows設(shè)為true。
-
SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
:隱藏status东羹,將用戶布局全屏顯示 -
SYSTEM_UI_FLAG_LAYOUT_STABLE
:結(jié)合上面屬性使用剂桥,在用戶布局全屏顯示的時(shí)候不隱藏status
代碼實(shí)現(xiàn)基于上面5.0以下的適配代碼。
private void addToolbarTransparent() {
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().setStatusBarColor(Color.TRANSPARENT);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
if (mStatusView == null) {
mStatusView = new View(this);
int screenWidth = getResources().getDisplayMetrics().widthPixels;
int screenHeight = getStatusBarHeight();
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(screenWidth,screenHeight);
mStatusView.setLayoutParams(layoutParams);
mStatusView.requestLayout();
ViewGroup systemContent = findViewById(android.R.id.content);
ViewGroup userContent = (ViewGroup) systemContent.getChildAt(0);
userContent.setFitsSystemWindows(true);
systemContent.addView(mStatusView, 0);
mStatusView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
最后属提,當(dāng)狀態(tài)欄字體需要從黑色變?yōu)闇\色時(shí)权逗,可以調(diào)用以下方法
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
}
總結(jié):
- 應(yīng)用場景總結(jié):
- 當(dāng)toolbar為單一顏色且只需適配5.0以上機(jī)型時(shí),用第一種方法,直接將status設(shè)為應(yīng)用狀態(tài)欄顏色斟薇,省時(shí)省力火惊。
- 當(dāng)需要適配低版本時(shí)或者需要status設(shè)為圖片背景時(shí),用第二種方法奔垦,雖然麻煩一點(diǎn),但靈活性相對高一些尸疆。
- 屬性總結(jié):
-
setFitsSystemWindows:即用戶視圖(user_content)為status預(yù)留空間椿猎,當(dāng)設(shè)為false時(shí),會是用戶視圖會全屏顯示寿弱。如下(相當(dāng)難看)
image - getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) : 這個(gè)標(biāo)簽會將status設(shè)為半透明犯眠,在高版本機(jī)型中是半透明形式顯示,但在低版本中是全透明(通常用于適配低版本手機(jī))症革。筆者測試小米5.0是全透明顯示筐咧。
- DecorView的結(jié)構(gòu),上面多次用到DecorView噪矛,那么DecorView的結(jié)構(gòu)是如何的呢量蕊?我們通過以下代碼找出其結(jié)構(gòu)關(guān)系
private void printView(ViewGroup viewGroup) {
Log.d(TAG, "printView: viewgroup:" + viewGroup.getClass().getSimpleName());
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
Log.d(TAG, "printView: childView:" + childView.getClass().getSimpleName());
}
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View childView = viewGroup.getChildAt(i);
if (childView instanceof ViewGroup) {
printView((ViewGroup) childView);
}
}
}
// onCreate()中調(diào)用
printView((ViewGroup)getWindow().getDecorView());
// 打印日志如下
printView: viewgroup:DecorView
printView: childView:LinearLayout
printView: viewgroup:LinearLayout
printView: childView:ViewStub
printView: childView:FrameLayout
printView: viewgroup:FrameLayout
printView: childView:FitWindowsLinearLayout
printView: viewgroup:FitWindowsLinearLayout
printView: childView:ViewStubCompat
printView: childView:ContentFrameLayout
printView: viewgroup:ContentFrameLayout
printView: childView:View
printView: childView:LinearLayout
printView: viewgroup:LinearLayout
printView: childView:Toolbar
printView: viewgroup:Toolbar
printView: childView:AppCompatTextView
根據(jù)打印日志畫出DecorView的結(jié)構(gòu)圖,如下:
在設(shè)置透明狀態(tài)欄的方法中艇挨,我們用下面方法找到的布局屬于那一層呢残炮?
ViewGroup systemContent = findViewById(android.R.id.content);
ViewGroup userContent = (ViewGroup) systemContent.getChildAt(0);
一樣用日志打出
Log.d(TAG, "addToolbarTransparent: systemContent / " + systemContent.getClass().getSimpleName());
Log.d(TAG, "addToolbarTransparent: userContent / " + userContent.getClass().getSimpleName());
// 輸出日志如下
addToolbarTransparent: systemContent / ContentFrameLayout
addToolbarTransparent: userContent / LinearLayout
systemContent對應(yīng)ContentFrameLayout,而userContent對應(yīng)LinearLayout缩滨,userContent的布局取決于你注入的布局文件的父布局势就,通過這些關(guān)系我們可以得出我們第二種方法的思路是 將ContentFrameLayout下的View設(shè)為透明,然后添加自定義View StatusView代替它脉漏。且userContent.setFitSystemWindow(true)
表示的就是為View預(yù)留空間苞冯。
參考文章中有一個(gè)方法實(shí)現(xiàn)是在userContent
層添加StatusView,經(jīng)過以下分析侧巨,如果注入的布局最外層不是LinearLayout的話舅锄,會達(dá)不到我們想要的效果,所以應(yīng)該在systemtContent
添加刃泡。
示例代碼傳送門:github地址