現(xiàn)在越來(lái)越多的軟件都開(kāi)始使用沉浸式狀態(tài)欄了,下面總結(jié)一下沉浸式狀態(tài)欄的兩種使用方法
注意!沉浸式狀態(tài)欄只支持安卓4.4及以上的版本
狀態(tài)欄:4.4上是漸變色,5.0上是完全透明,本文模擬器為4.4演示
效果圖:

注意!兩種方法的區(qū)別:
第一種:為頂部欄跟隨當(dāng)前activity的布局文件的背景的顏色,使用方便,不過(guò)也有點(diǎn)問(wèn)題就是,如果有底部虛擬導(dǎo)航鍵的話(huà),導(dǎo)航鍵的背景跟頂部的顏色一樣,比如:
第二種:是通過(guò)設(shè)置頂部欄的顏色來(lái)顯示的,可以解決第一種的不足,比如:

第一種使用方法:
第一、首先在values、values-v19、values-v21文件夾下的styles.xml都設(shè)置一個(gè) Translucent System Bar 風(fēng)格的Theme,如下圖:

values/style.xml:
<style name="TranslucentTheme" parent="AppTheme"> <!--在Android 4.4之前的版本上運(yùn)行,直接跟隨系統(tǒng)主題--></style>values-v19/style.xml:<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item></style>
values-v21/style.xml:
<style name="TranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">true</item> <!--Android 5.x開(kāi)始需要把顏色設(shè)置透明,否則導(dǎo)航欄會(huì)呈現(xiàn)系統(tǒng)默認(rèn)的淺灰色--> <item name="android:statusBarColor">@android:color/transparent</item></style>
第二、在清單文件中配置需要沉浸式狀態(tài)欄的activity加入theme
<activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" /><activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" />
第三、在Activity的布局文件中的跟布局加入“android:fitsSystemWindows=”true””,但是,這里需要區(qū)分一下,就是背景是圖片還是純色:
1.當(dāng)背景為圖片時(shí),布局可以這么寫(xiě):
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/imgs_bj" android:fitsSystemWindows="true"></RelativeLayout>
效果:

2.當(dāng)背景為純色,我們需要對(duì)布局劃分一下,標(biāo)題布局與內(nèi)容布局,先把根布局背景設(shè)置成標(biāo)題布局的背景色,然后標(biāo)題背景色可以不用設(shè)置直接使用根布局的背景色,最后內(nèi)容布局背景色設(shè)置為白色
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" //根布局背景設(shè)置成“標(biāo)題布局”想要的顏色 android:fitsSystemWindows="true" android:orientation="vertical"> <!--標(biāo)題布局--> <RelativeLayout android:layout_width="match_parent" android:layout_height="55dp" android:background="@color/color_31c27c"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="這是標(biāo)題" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> <!--內(nèi)容布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" //內(nèi)容區(qū)域背景設(shè)置成白色 android:gravity="center" android:orientation="vertical"> <Button android:layout_marginTop="120dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="8dp" android:text="顯示信息" android:onClick="showMsg" /> </LinearLayout></LinearLayout>
效果圖:

好了,以上就是沉浸式狀態(tài)欄實(shí)現(xiàn)的全過(guò)程,但是還有一點(diǎn)值得注意的就是,如果我們activity比較多,每一個(gè)頁(yè)面都添加Android:fitsSystemWindows="true" 比較麻煩,我們需要改動(dòng)一下:
寫(xiě)一個(gè)基類(lèi)BaseColorActivity.class,代碼如下:
public abstract class BaseColorActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //這一行注意!看本文最后的說(shuō)明!!!! supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(getLayoutResId());//把設(shè)置布局文件的操作交給繼承的子類(lèi) ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT); View parentView = contentFrameLayout.getChildAt(0); if (parentView != null && Build.VERSION.SDK_INT >= 14) { parentView.setFitsSystemWindows(true); } } /** * 返回當(dāng)前Activity布局文件的id * * @return */ abstract protected int getLayoutResId();}然后需要沉浸狀態(tài)欄的activity繼承該基類(lèi):
public class ColorActivity extends BaseColorActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //這里不需要寫(xiě)setContentView()! } @Override protected int getLayoutResId() { //onCreate的方法中不需要寫(xiě)setContentView(),直接把當(dāng)前activity的布局文件在這里返回就行了! return R.layout.activity_color; }}然后需要沉浸狀態(tài)欄的activity的布局文件中就可以把a(bǔ)ndroid:fitsSystemWindows="true"這行代碼給省略了!
第二種使用方法(未完):
寫(xiě)個(gè)工具類(lèi)StatusBarCompat.class:
public class StatusBarCompat { private static final int INVALID_VAL = -1; private static final int COLOR_DEFAULT = Color.parseColor("#20000000"); @TargetApi(Build.VERSION_CODES.LOLLIPOP) public static void compat(Activity activity, int statusColor) { //當(dāng)前手機(jī)版本為5.0及以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { activity.getWindow().setStatusBarColor(statusColor); } return; } //當(dāng)前手機(jī)版本為4.4 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { int color = COLOR_DEFAULT; ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content); if (statusColor != INVALID_VAL) { color = statusColor; } View statusBarView = new View(activity); ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity)); statusBarView.setBackgroundColor(color); contentView.addView(statusBarView, lp); } } public static void compat(Activity activity) { compat(activity, INVALID_VAL); } public static int getStatusBarHeight(Context context) { int result = 0; int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources().getDimensionPixelSize(resourceId); } return result; }}使用方法:
在當(dāng)前activity的onCreate中,調(diào)用方法StatusBarCompat.compat就可以了:
//第二個(gè)參數(shù)是想要設(shè)置的顏色StatusBarCompat.compat(this, Color.RED);
如果嫌每個(gè)activity都要寫(xiě)有點(diǎn)麻煩,那就寫(xiě)個(gè)基類(lèi)來(lái)完成這一步:
public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { supportRequestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); StatusBarCompat.compat(this, Color.RED); }}然后每個(gè)activity的頁(yè)面繼承該BaseActivity就可以了!
關(guān)于上面代碼中提示注意的那個(gè)地方的說(shuō)明:
隱藏系統(tǒng)title注意的兩點(diǎn):
1、繼承AppCompatActivity時(shí)使用:
supportRequestWindowFeature(Window.FEATURENOTITLE)
2、繼承activity時(shí)使用:
requestWindowFeature(Window.FEATURENOTITLE)
文本相關(guān)下載:點(diǎn)擊免費(fèi)下載源碼及apk文件
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注