實(shí)現(xiàn)狀態(tài)欄沉浸式(透明狀態(tài)欄)的幾種方式:
1、通過(guò)第三方j(luò)ar包
2、設(shè)置狀態(tài)欄透明,頂部View拉伸一個(gè)狀態(tài)欄高度(重點(diǎn)講)
3、利用toolbar中的colorPrimaryDark設(shè)置狀態(tài)欄顏色
注意:需要注意適配的問(wèn)題,狀態(tài)欄透明需要Android4.4以上,而直接改狀態(tài)欄顏色需要Android5.0以上。
1、通過(guò)第三方j(luò)ar包
上網(wǎng)搜SystemBarTint,這里不做介紹了
2、設(shè)置狀態(tài)欄透明,頂部View拉伸一個(gè)狀態(tài)欄高度
這種方法是思路是這樣的,將狀態(tài)欄設(shè)置成透明,相當(dāng)于全屏,這個(gè)時(shí)候狀態(tài)欄的內(nèi)容會(huì)和布局的內(nèi)容重疊,為頂部View拉伸一個(gè)狀態(tài)欄的高度可去掉這種重疊。
設(shè)置狀態(tài)欄兩種方法:
第一種:在styles.xml文件定義主題,在A(yíng)ndroidManifest.xml中對(duì)需要沉浸式的activity引用主題。
比如這樣,設(shè)置無(wú)標(biāo)題,狀態(tài)欄透明(如果繼承appcomtActivity的話(huà),有可能設(shè)置無(wú)標(biāo)題要用另一種方式,這個(gè)先不討論,自己找方法)
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="android:Theme.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<style name="TranslucentTheme" parent="AppBaseTheme">
<!--在A(yíng)ndroid 4.4之前的版本上運(yùn)行-->
<item name="android:windowNoTitle">true</item>
</style>
</resources>
考慮到適配問(wèn)題,創(chuàng)建文件夾values-v19,同時(shí)創(chuàng)建styles.xml文件,用來(lái)區(qū)分4.4以上情況,values-v19中加上<item name="android:windowTranslucentStatus">true</item>
<style name="TranslucentTheme" parent="AppBaseTheme">
<!--在A(yíng)ndroid 4.4以上的版本上運(yùn)行-->
<item name="android:windowNoTitle">true</item>
<item name="android:windowTranslucentStatus">true</item>
</style>
然后在A(yíng)ndroidManifest文件中設(shè)置主題,如:
<activity
android:name=".CommunityActivity"
android:theme="@style/TranslucentTheme"
android:label="Community" >
</activity>
這種方法不好的一點(diǎn)就是,在A(yíng)ndroid5.0以上即使設(shè)置了透明,我這仍會(huì)出現(xiàn)半透明的遮罩,下面這種方法可以通過(guò)修改狀態(tài)欄顏色解決,看個(gè)人喜好吧。
第二種:直接在java代碼中實(shí)現(xiàn),放在activity的setContentView后面,上代碼:
//Android5.0以上
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);
}
//Android4.4以上,5.0以下
else if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.KITKAT){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
拉伸View一個(gè)狀態(tài)欄高度的兩種方法:
第一種:在頂部的View布局(一般是Toolbar或者自定義的布局)中加入:android:fitsSystemWindows="true"
且需android:layout_height="wrap_content"
這樣系統(tǒng)會(huì)自動(dòng)幫我們將該View設(shè)置一個(gè)paddingTop,解決重疊。
但是在一些有EditText的情況下可能會(huì)出現(xiàn)問(wèn)題,這個(gè)留到后面講。
第二種:在Java代碼中實(shí)現(xiàn),在activity中敲下面代碼:
//計(jì)算狀態(tài)欄高度,加到View上
int statusBarHeight = StatusBarUtil.getStatusBarHeight(this);
mTopLayout.setPadding(0,statusBarHeight,0,0);
獲取狀態(tài)欄的方法getStatusBarHeight:
public static int getStatusBarHeight(Context context) {
Resources resources = context.getResources();
int resourceId = resources.getIdentifier("status_bar_height", "dimen","android");
int height = resources.getDimensionPixelSize(resourceId);
return height;
}
mTopLayout是我頂部的View(Toolbar同理),上面代碼要在findViewById后才能調(diào)用。
我個(gè)人看來(lái),這種方法更可控,后面會(huì)講到。
可能出現(xiàn)的問(wèn)題及解決方案:
-
有EditText的情況:
如果是通過(guò)android:fitsSystemWindows="true"來(lái)自動(dòng)拉伸頂部的view的話(huà),那么在有EditText的情況下,由于會(huì)彈出軟鍵盤(pán),所以這里測(cè)量的拉伸距離會(huì)增加一個(gè)軟鍵盤(pán)的高度。
解決辦法有三種:
一是在A(yíng)ndroidManifest文件設(shè)置Activity的軟鍵盤(pán)的屬性android:windowSoftInputMode="adjustPan"這樣整個(gè)布局向上平移,也就不存在拉伸的問(wèn)題。
二是把android:fitsSystemWindows="true"放在根布局下,把根布局的背景色改成想要的狀態(tài)欄色,不過(guò)這可能會(huì)有個(gè)問(wèn)題,彈起鍵盤(pán)的過(guò)程中可能會(huì)閃屏,顏色是根布局背景色(本身就會(huì)閃屏,只不過(guò)原本是白色,看不出)??梢赃@么解決,弄一張背景圖,圖片上邊緣是狀態(tài)欄色,下面是白色就可以了,嫌麻煩的話(huà)基本可以放棄了。
三是不用設(shè)置android:fitsSystemWindows="true"這種方法,而是上面提到的第二種方法,在java代碼中實(shí)現(xiàn),個(gè)人比較推薦。
-
上一個(gè)activity有EditText的情況:
若當(dāng)前activity設(shè)置的是android:fitsSystemWindows="true",那么在上一個(gè)activity的軟鍵盤(pán)打開(kāi)狀態(tài),直接退回當(dāng)前activity,會(huì)出現(xiàn)頂部view過(guò)度拉伸的現(xiàn)象,如下圖:

解決方法是當(dāng)前activity用上面提到的第二張方法,在java代碼實(shí)現(xiàn)。
3、利用toolbar中的colorPrimaryDark設(shè)置狀態(tài)欄顏色
這方法只要在網(wǎng)上搜Toolbar的相關(guān)教程,都有相關(guān)的內(nèi)容,主要是通過(guò)直接設(shè)置狀態(tài)欄的顏色來(lái)達(dá)到效果,不過(guò)在低版本無(wú)效,只適用于A(yíng)ndroid5.0以上。
基于學(xué)習(xí)需要,作如上總結(jié),希望能幫助到需要的人以及提醒自己不斷完善,共同進(jìn)步。