?????? 在這個(gè)行業(yè)上班1年零2個(gè)月了,一直都沒(méi)有好好的總結(jié)在這個(gè)行業(yè)的技術(shù)能力瞻鹏,為了好好的練就自己的技術(shù),也為了各位網(wǎng)友鹿寨,以互利共贏的態(tài)度新博,來(lái)好好的維護(hù)博客(*^__^*)
??????? 前段時(shí)間被一個(gè)狀態(tài)欄的問(wèn)題困擾了下,在4.4的手機(jī)上能設(shè)置狀態(tài)欄脚草,但在7.0的手機(jī)上面卻設(shè)置不了了赫悄,雖然經(jīng)過(guò)一番百度,把問(wèn)題給解決了,但只停留在吧問(wèn)題解決了埂淮,沒(méi)有看看到底是什么原因姑隅,于是記錄一下?tīng)顟B(tài)欄的問(wèn)題,以后如果遇到了倔撞,也能快速的解決讲仰。
狀態(tài)欄介紹:
狀態(tài)欄在4.4之前是沒(méi)有的,在4.4(API19)到5.0(API21)痪蝇,通過(guò)設(shè)置FLAG_TRANSLUCENT_STATUS鄙陡,并添加和StatusBar一樣高度的View才能實(shí)現(xiàn)沉浸式的狀態(tài)欄,否則躏啰,內(nèi)容會(huì)跑到狀態(tài)欄上去(如果要這種效果那就讓他跑吧(*^__^*) 嘻嘻……)趁矾。
但在5.0(API21)之后,出現(xiàn)了設(shè)置狀態(tài)欄顏色的接口给僵,直接設(shè)置就可以了毫捣,先介紹到這里,下面我來(lái)一步一步的分析帝际,扒開(kāi)他所謂的面紗培漏。
先在4.4的手機(jī)上試驗(yàn):
當(dāng)在java中添加:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
或者在theme中加屬性(在values-v19 中添加):
<item name="android:windowTranslucentStatus">true</item
效果:
當(dāng)然,想要這種效果也不錯(cuò)
但加如下代碼(在StatusBar上添加View):
//獲取windowphone下的decorView
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
intcount? ? = decorView.getChildCount();
//判斷是否已經(jīng)添加了statusBarView
//? ? ? ? if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
//? ? ? ? ? ? decorView.getChildAt(count - 1).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
//? ? ? ? } else {
//新建一個(gè)和狀態(tài)欄高寬的view
View statusView =createStatusBarView(activity,color,alpha);
decorView.addView(statusView);
//? ? ? ? }
ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
//rootview不會(huì)為狀態(tài)欄留出狀態(tài)欄空間
ViewCompat.setFitsSystemWindows(rootView,true);
rootView.setClipToPadding(true);
private staticViewcreateStatusBarView(Activity activity, intcolor, intalpha) {
// 繪制一個(gè)和狀態(tài)欄一樣高的矩形
View statusBarView =newView(activity);
LinearLayout.LayoutParams params =
newLinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,getStatusBarHeight(activity));
statusBarView.setLayoutParams(params);
statusBarView.setBackgroundColor(color);
return statusBarView;
}
private static intgetStatusBarHeight(Context context) {
// 獲得狀態(tài)欄高度
intresourceId = context.getResources().getIdentifier("status_bar_height","dimen","android");
return context.getResources().getDimensionPixelSize(resourceId);
}
內(nèi)容正常顯示
在7.1手機(jī)上試驗(yàn):
由于在5.0以后胡本,能更容易的修改狀態(tài)欄牌柄,setStatusBarColor()可設(shè)置狀態(tài)欄顏色,對(duì)應(yīng)的在主題中修改是android:statusBarColor(values-v21)
/**
* Sets the color of the status bar to {@codecolor}.
*
* For this to take effect,
* the window must be drawing the system bar backgrounds with
* {@linkandroid.view.WindowManager.LayoutParams#FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS} and
* {@linkandroid.view.WindowManager.LayoutParams#FLAG_TRANSLUCENT_STATUS} must not be set.
*
* If {@codecolor} is not opaque, consider setting
* {@linkandroid.view.View#SYSTEM_UI_FLAG_LAYOUT_STABLE} and
* {@linkandroid.view.View#SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN}.
*
* The transitionName for the view background will be "android:status:background".
*
*/
public abstract voidsetStatusBarColor(@ColorIntintcolor);
意思是必須要設(shè)置FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS侧甫,并且去掉FLAG_TRANSLUCENT_STATUS珊佣,按他的意思在7.1的手機(jī)上看下效果
但要鋪滿(mǎn)呢,只需要添加FLAG_TRANSLUCENT_STATUS就可以了披粟,效果:
總結(jié):
沉浸式狀態(tài)欄就是這么簡(jiǎn)單咒锻,只需要靜下心來(lái),分析下各種情況守屉,有時(shí)看別人這么寫(xiě)惑艇,很納悶,但看下源碼拇泛,原來(lái)滨巴,源碼里的注釋就說(shuō)明了,必須要這么做俺叭,所以恭取,看源碼很有必要!后續(xù)熄守,我會(huì)做一個(gè)工具包蜈垮,方便大家使用耗跛。