Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 資訊

Android Translucent System Bar 的最佳實踐

編輯:Android 資訊

近幾天打算抽空總結 Android 一些系統 UI 的實踐用法,於是動手建了個庫 AndroidSystemUiTraining,邊撸代碼邊寫總結。今天開寫第一篇,對 Translucent System Bar 的實踐做個總結。說起這個特性,可能有些朋友還比較陌生,先簡單介紹一下。

如上圖,Android 4.4 之前,打開 App 時總能看到系統頂部那條黑乎乎的通知欄,讓 App 顯得突兀。於是從 Android 4.4 開始,系統引入了 Translucent System Bar 特性,用來彌補通知欄的突兀(估計也是向 iOS 學習,因為 iOS 早就有這特性)。我們先看看引入該特性後的變化。下面截取了 中華萬年歷的天氣預報界面 與 QQ 音樂主界面 的效果(兩者實現方式略有區別,下文細講):

中華萬年歷

QQ 音樂

可以看到,系統通知欄與 App 界面融為一體,告別了黑乎乎的通知欄。關於 Translucent System Bar 就簡介到此。

工程簡介

先簡單介紹工程結構,核心部分已圈出,待我逐一講解:

  • 主要操作都在 style.xml 和 AndroidManifest.xml 中,Activity 裡沒有任何涉及 Translucent System Bar 設置的代碼,可以忽略不看。
  • ColorTranslucentBarActivity 和 ImageTranslucentBarActivity 分別展示兩種不同實現方式的效果。
  • 要在 Activity 中使用該特性,需先在 AndroidManifest 中為指定 Activity 設置 Theme。但注意:不能直接在 values/style.xml 裡自定義 Translucent System Bar 的 Theme,因為該特性僅兼容 Android 4.4 及以上平台,直接聲明會報錯。有些開發者會在代碼中判斷 SDK 版本再設置 Theme,雖也能實現,但個人並不推崇。我採用的辦法是建立多個 SDK 版本的 values 文件夾,讓系統根據版本自動選擇合適的 Theme——工程裡分別有 values、values-v19、values-v21。

第一種方式

第一種方式需做三步設置:

1、在 values、values-v19、values-v21 的 style.xml 中都設置一個 Translucent System Bar 風格的 Theme:

values/style.xml

<style name="ImageTranslucentTheme" parent="AppTheme"> <!--在 Android 4.4 之前的版本上運行,直接跟隨系統主題--> </style>

values-v19/style.xml

<style name="ImageTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">true</item> <item name="android:windowTranslucentNavigation">true</item> </style>

values-v21/style.xml

<style name="ImageTranslucentTheme" 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>

需要注意:無論在哪個 SDK 版本的 values 目錄下設置主題,都應在最基本的 values 下設置一個同名主題,才能確保 App 正常運行於 Android 4.4 以下設備,否則會報找不到 Theme 的錯誤。

2、在 AndroidManifest.xml 中對指定 Activity 設置 theme:

<activity android:name=".ui.ImageTranslucentBarActivity" android:label="@string/image_translucent_bar" android:theme="@style/ImageTranslucentTheme" />

3、在 Activity 布局文件中設置背景圖片,同時把 android:fitsSystemWindows 設為 true:

activity_image_translucent_bar.xml

<?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="@mipmap/env_bg" android:fitsSystemWindows="true"> </RelativeLayout>

到此第一種方式完成,效果如下:

就跟中華萬年歷的天氣預報界面一樣,整個導航欄融入 App 界面,背景圖片填滿全屏,看起來舒服多了。關於 android:fitsSystemWindows 的設置,後面會細講。接下來看第二種方式。

方式二

相比中華萬年歷,QQ 音樂採用的是另一種方式,它把 App 的 Tab 欄與系統導航欄分開設置:

由於它的 Tab 欄是純色的,只要把系統通知欄顏色設成與 Tab 欄一致即可,實現上比方法一簡單很多。同樣要在不同 SDK 版本的 values 下創建同名 theme,在 values-v21 下設置系統導航欄顏色:

values-v21/style.xml

<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <item name="android:windowTranslucentStatus">false</item> <item name="android:windowTranslucentNavigation">true</item> <item name="android:statusBarColor">@color/color_31c27c</item> </style>

再到 ColorTranslucentBarActivity 的布局文件 activity_color_translucent_bar.xml 中設置 Tab 欄顏色:

<?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: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="QQ Music" android:textColor="@android:color/white" android:textSize="20sp" /> </RelativeLayout> </LinearLayout>

到此,就能得到和 QQ 音樂主界面一樣的效果了:

至此,Translucent System Bar 的兩種實現方式就介紹完了。

android:fitsSystemWindows 的「踩坑」

大家可能留意到,所有實現 Translucent System Bar 效果的 Activity 都要在根布局設置 android:fitsSystemWindows="true"。它的作用是不讓系統導航欄與 App UI 重疊而導致交互問題。看下面對比就清楚了。

有 fitsSystemWindows 設置

沒有 fitsSystemWindows 設置

註:以上演示效果借助了我的另一個開源項目 AndroidAlbum。

這樣一來,若有 10 個 Activity 要實現該效果,就得在 10 個布局文件裡設置,非常麻煩。於是我想:能否在 theme 中加上如下設置?

<item name="android:fitsSystemWindows">true</item>

試了發現果真可行,所有要實現 Translucent System Bar 的 Activity 只需設置這個 theme 即可,改起來很方便。可惜後來出現了一個 BUG,讓我不得不老老實實回到布局文件裡設置:

Toast 打印出來的文字竟然向上偏移了。這也是我疏忽的地方:在布局文件中設置是對 View 生效,而在 theme 中設置則是對 Window 生效,兩者實現上並不一樣。所以最終只能改回原來的方式。

實踐總結

最後做個小總結:

  • 方式一適用於 App 中沒有導航欄、且整體背景是一張圖片的界面;
  • 方式二適用於 App 中導航欄顏色為純色的界面;
  • android:fitsSystemWindows 要在布局文件中設置,不要放到 theme 裡。

怎樣,介紹到這裡,你會用 Translucent System Bar 了嗎?趕快到你的 App 裡試試吧!

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