Android狀態(tài)欄顏色修改
名稱糾結(jié)
參考了一系列的文章,開頭都會(huì)闡述一下沉浸式狀態(tài)欄和狀態(tài)欄著色的區(qū)別。這里我用自己的理解來(lái)區(qū)分這兩種概念:很多播放器播放電影時(shí),會(huì)隱藏狀態(tài)欄,當(dāng)滑動(dòng)屏幕時(shí)又會(huì)出現(xiàn)狀態(tài)欄,這就是沉浸式。而在應(yīng)用界面看到StatusBar顏色和ActionBar(ToolBar)顏色一致的,則是通過(guò)改變狀態(tài)欄顏色實(shí)現(xiàn)的。
版本區(qū)別
在4.4之前狀態(tài)欄一直是黑色的,在4.4中帶來(lái)了 windowTranslucentStatus 這一特性,由此.4及其以上都是可以實(shí)現(xiàn)給狀態(tài)欄設(shè)置顏色。但是在5.0及其以上可以直接在主題中設(shè)置顏色,或者調(diào)用 Window 類中的 setStatusBarColor(int color) 來(lái)實(shí)現(xiàn),非常方便。所以需要有一種兼容兩個(gè)版本的實(shí)現(xiàn)方法。
實(shí)現(xiàn)改變狀態(tài)欄顏色
要想改變狀態(tài)欄顏色,也就是將顏色設(shè)置成和ActionBar(ToolBar)一樣。首先需要把StatusBar設(shè)置成透明
方案
實(shí)現(xiàn)大致的思路:透明狀態(tài)欄,并將狀態(tài)欄和DecorView重疊。實(shí)現(xiàn)狀態(tài)欄顏色與ActionBar(ToolBar)顏色一致。注意為了讓ActionBar(ToolBar)高度不變小,需要給paddingtop屬性設(shè)置StatusBar的高度值。
透明化狀態(tài)欄
Java實(shí)現(xiàn)方式:
//當(dāng)版本大于,等于5.0時(shí)
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//1.狀態(tài)欄在DecorView之上,其顏色跟隨ActionBar(ToolBar)改變
View decorView = getWindow().getDecorView();
//設(shè)置根View(DecorView)Flag
//SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN,狀態(tài)欄懸浮于Activity上
//SYSTEM_UI_FLAG_LAYOUT_STABLE,保持整個(gè)View穩(wěn)定, 常跟bar 懸浮, 隱藏共用, 使View不會(huì)因?yàn)镾ystemUI的變化而做layout
int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
//setSystemUiVisibility()通過(guò)Flag更改所謂SystemUI的屬性
decorView.setSystemUiVisibility(option);
//透明狀態(tài)欄
getWindow().setStatusBarColor(Color.TRANSPARENT);
}else if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//得到Window實(shí)例的屬性
WindowManager.LayoutParams localLayoutParams = getWindow().getAttributes();
//添加透明狀態(tài)欄Flag,就是利用了4.4添加的WindowTranslucentStatus特性
//此時(shí)布局將直接延伸到狀態(tài)欄下方
localLayoutParams.flags = (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS |
localLayoutParams.flags);
}
通過(guò)主題實(shí)現(xiàn)方式:
為了兼容4.4一下的版本:
<style name="TranslucentTheme" parent="AppTheme"/>
4.4版本:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
</style>
5.0及5.0以上版本:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowTranslucentStatus">true</item>
<item name="android:windowTranslucentNavigation">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
</style>
最后我們?cè)贏ndroidManifest.xml文件中給Activity或者Application設(shè)置主題屬性即可。
這時(shí)候出現(xiàn)問(wèn)題:
在5.0模擬器上,以及6.0真機(jī)上測(cè)試,發(fā)現(xiàn)狀態(tài)欄透明度并沒(méi)有達(dá)到百分之百,如下圖顯示。所以這里建議使用Java代碼實(shí)現(xiàn)狀態(tài)欄透明。
改變狀態(tài)欄顏色
通過(guò)第一步的透明化狀態(tài)欄,導(dǎo)致布局會(huì)延伸到StatusBar下方。這里利用這一特點(diǎn),為自定義的ToolBar增加一個(gè)paddingTop屬性,其值就是StatusBar的高度。這樣一來(lái),改變了狀態(tài)欄的顏色,同時(shí)與ToolBar顏色一致。
首先在values/dimens添加padingTop的值,也就是StatusBar的高度。
為了兼容4.4以前的版本:
<dimen name="padding_top">0dp</dimen>
4.4以后的版本:
<dimen name="padding_top">25dp</dimen>
ToolBar:
<android.support.v7.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:id="@+id/id_tool_bar"
android:paddingTop="@dimen/padding_top"
android:minHeight="?android:actionBarSize" />
從上面代碼看到,設(shè)置了一個(gè)屬性android:minHeight,但是發(fā)現(xiàn)預(yù)想的結(jié)果和實(shí)際還是有區(qū)別的。由于增加了android:paddingTop屬性,整個(gè)ActionBarSize會(huì)減去這個(gè)屬性的值,最終的值就是實(shí)際效果的高度,如下圖:
由于我們無(wú)法在編寫布局文件前得到主題樣式定制的ActionBarSize高度,通過(guò)?android:actionBarSize來(lái)獲取。所以沒(méi)有辦法在布局文件中直接確定ToolBar的高度。那么如何在代碼中獲取ActionBar的高度呢?并且在代碼中還需要獲取paddingTop屬性的值。
float toolBarPaddingTop = (int) (getResources().getDimension(R.dimen.padding_top));
TypedArray ta = obtainStyledAttributes(new int[]{android.R.attr.actionBarSize});
float actionBarHeight = ta.getDimension(0, 0);
int toolBarHeight = (int) (toolBarPaddingTop + actionBarHeight);
mToolBar.setMinimumHeight(toolBarHeight);
最終實(shí)現(xiàn)的效果:
如果一個(gè)activity的布局文件頂部是一張圖片,或者整個(gè)就是一張圖片,那么不需要設(shè)置paddingTop屬性。直接讓其拉伸到StatusBar下方。實(shí)現(xiàn)效果如下:
如何在代碼中獲取準(zhǔn)確的狀態(tài)欄的高度
public int getStatusBarHeight(Context context) {
int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
return context.getResources().getDimensionPixelSize(resourceId);
}
代碼分析:使用getIdentifier()
方法可以方便的獲各應(yīng)用包下的指定資源ID。第一個(gè)參數(shù)ID名,第二個(gè)參數(shù)資源屬性,第三個(gè)參數(shù)包名。
注意
在5.0新增屬性中:
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
colorPrimary是ActionBar顏色,colorPrimaryDark是StatusBar顏色屬性,ToolBar顏色需要用它的background屬性來(lái)指定顏色。
目標(biāo)
實(shí)現(xiàn)狀態(tài)欄顏色改變還有另一種方法,利用fitSystemWindows屬性,但是本身功底不夠,看不太懂...