Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 沉浸式狀態列實作:兩種透明化方案完整教學

編輯:關於 Android 程式設計

越來越多 App 採用沉浸式狀態列,讓狀態列與頁面頂部融為一體。以下整理兩種常見做法與各自的取捨。

先提醒兩點:沉浸式狀態列需要 Android 4.4 以上才支援;在 4.4 上狀態列呈現漸層半透明,5.0 以上則可設定為完全透明。本文以 4.4 模擬器示範。

兩種方案的差異

第一種做法是讓狀態列直接沿用該 Activity 佈局檔的root背景色,設定簡單,但有一個副作用:如果裝置有底部虛擬導覽鍵,導覽鍵的背景會跟頂部同色,視覺上不一定理想。

 

第二種做法是透過程式碼主動指定頂部顏色,可以避開上述問題。

方案一:改用 Translucent Theme

首先在 values、values-v19、values-v21 三個資料夾的 styles.xml 中各自定義一個 TranslucentTheme 主題:

values/style.xml:

 <style name="TranslucentTheme" parent="AppTheme"> <!--在Android 4.4之前的版本上運行,直接跟隨系統主題--> </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開始需要把顏色設置透明,否則導航欄會呈現系統默認的淺灰色--> <item name="android:statusBarColor">@android:color/transparent</item> </style> 

接著在清單檔中為需要沉浸效果的 Activity 套用該主題:

 <activity android:name=".ImageActivity" android:theme="@style/TranslucentTheme" /> <activity android:name=".ColorActivity" android:theme="@style/TranslucentTheme" /> 

最後在 Activity 佈局的根節點加上 android:fitsSystemWindows="true"。這裡要區分背景是圖片還是純色:

1、背景為圖片時,直接讓根布局填滿並套用 fitsSystemWindows 即可:

 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://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、背景為純色時,建議把畫面切成標題層與內容層:根布局的背景設成標題想用的顏色,標題區不再單獨設背景,內容區則設為白色,這樣狀態列下面就自然接上標題色。

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorPrimary" //根布局背景設置成“標題布局”想要的顏色 android:fitsSystemWindows="true" android:orientation="vertical"> <!--標題布局--> <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="這是標題" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> <!--內容布局--> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/white" //內容區域背景設置成白色 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> 

問題來了:如果 App 頁面很多,每個佈局都要手動加上 fitsSystemWindows 實在麻煩。這時可以抽一個基類,在基類裡用程式碼設定:

 public abstract class BaseColorActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //這一行注意!看本文最後的說明!!!! supportRequestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(getLayoutResId());//把設置布局文件的操作交給繼承的子類 ViewGroup contentFrameLayout = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT); View parentView = contentFrameLayout.getChildAt(0); if (parentView != null && Build.VERSION.SDK_INT >= 14) { parentView.setFitsSystemWindows(true); } } /** * 返回當前Activity布局文件的id * * @return */ abstract protected int getLayoutResId(); } 

要沉浸效果的 Activity 直接繼承它,並把佈局 id 回傳即可:

 public class ColorActivity extends BaseColorActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //這裡不需要寫setContentView()! } @Override protected int getLayoutResId() { //onCreate的方法中不需要寫setContentView(),直接把當前activity的布局文件在這裡返回就行了! return R.layout.activity_color; } } 

如此一來,子頁面的佈局檔就不必再寫 fitsSystemWindows 了。

方案二:寫一個工具類動態上色

 

這個做法的核心是:5.0 以上直接呼叫 setStatusBarColor;4.4 上則手動在內容檢視頂端插入一個與狀態列等高的 View 當作「假狀態列」:

 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) { //當前手機版本為5.0及以上 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { if (statusColor != INVALID_VAL) { activity.getWindow().setStatusBarColor(statusColor); } return; } //當前手機版本為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; } } 

呼叫方式很簡單,在 Activity 的 onCreate 中帶入想要的顏色:

 //第二個參數是想要設置的顏色 StatusBarCompat.compat(this, Color.RED);

懶得每頁都寫,一樣可以抽成基類:

 public class BaseActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { supportRequestWindowFeature(Window.FEATURE_NO_TITLE); super.onCreate(savedInstanceState); StatusBarCompat.compat(this, Color.RED); } } 

補充:隱藏系統標題列要注意的地方

前面程式碼中提醒的那一行,兩種繼承情境的寫法不同:繼承 AppCompatActivity 時用 supportRequestWindowFeature(Window.FEATURE_NO_TITLE);繼承 Activity 時用 requestWindowFeature(Window.FEATURE_NO_TITLE)。此外,這行必須放在 super.onCreate 之前或之後的固定位置才有效,順序錯了會直接拋出例外。

 supportRequestWindowFeature(Window.FEATURENOTITLE)
 requestWindowFeature(Window.FEATURENOTITLE) 

以上就是沉浸式狀態列的兩種實作路徑。想要省事就挑方案一,需要精細控制每頁顏色則用方案二。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved