android 任務欄Translucent System Bar 設置

Translucent System Bar 的最佳實踐

近幾天準備抽空總結Android一些系統UI的實踐使用,於是開始動手建了一個庫 AndroidSystemUiTraining ,邊擼代碼邊寫總結

今天開寫第一篇,對 Translucent System Bar 的實踐做一些總結。說起 Translucent System Bar 的特性,可能有些朋友還比較陌生,這裏做一下簡單的介紹。


Android 4.3豌豆莢

看上圖,Android 4.4之前,即使我們打開手機app,我們還總是能看到系統頂部那條黑乎乎的通知欄,這樣會使得app稍顯突兀。於是Android 4.4開始,便引入了Translucent System Bar的系特性,用於彌補系統通知欄突兀之處。(估計也是向ios學習,因爲ios一大早就有這個特性)。我們先來看看 Translucent System Bar 新特性引入後,發生了什麼樣的變化。下面截取了 中華萬年曆的天氣預報界面QQ音樂主界面 的效果(兩個界面的效果實現 Translucent System Bar 的方式有些區別,下文會細講)


中華萬年曆

QQ音樂

可以看到,系統的通知欄和app界面融爲一體,媽媽再也不用面對黑乎乎的通知欄了。有關 Translucent System Bar 的特性就暫且介紹到此。

工程簡介

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


工程結構
  • 主要的操作都在style.xml 和 AndroidManifest.xml 中,Activity裏面沒有任何涉及到Translucent System Bar設置的代碼,所以可以忽略不看。

  • ColorTranslucentBarActivity 和 ImageTranslucentBarActivity 分別用於展示兩種不同實現方式的效果

  • 要在Activity中使用 Translucent System Bar 特性,首先需要到AndroidManifest中爲指定的Activity設置Theme。但是需要注意的是,我們不能直接在values/style.xml直接去自定義 Translucet System Bar 的Theme,因爲改特性僅兼容 Android 4.4 開始的平臺,所以直接在values/style.xml聲明引入,工程會報錯。有些開發者朋友會在代碼中去判斷SDK的版本,然後再用代碼設置Theme。雖然同樣可以實現效果,但個人並不推崇這種做法。我所採取的方法則是建立多個SDK版本的values文件夾,系統會根據SDK的版本選擇合適的Theme進行設置。大家可以看到上面我的工程裏面有valuesvalues-v19values-v21

第一種方式

第一種方式,需要做下面三步設置

1、在valuesvalues-v19values-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="http://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>

到此,第一種實現方式完成,大家可以看看下面的效果


ImageTranslucentTheme效果

就跟中華萬年曆的天氣預報效果界面一樣,系統的整個導航欄都融入了app的界面中,背景圖片填滿了整個屏幕,看起來舒服很多。這裏還有一個android:fitsSystemWindows設置需要注意的地方,後面會在細講。接下來看第二種實現。

方式二

相比中華萬年曆,QQ音樂採用的是另外一種實現的方式,它將app的Tab欄和系統導航欄分開來設置。


QQ音樂效果風格

由於它的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="http://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音樂主界面一樣的效果了。


QQ音樂界面實現效果

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

android:fitsSystemWindows的“踩坑”

通過前面的兩種方式,大家估計會留意到一個地方,就是所有實現 Translucent System Bar 效果的Activity,都需要在根佈局裏設置 android:fitsSystemWindows="true" 。設置了該屬性的作用在於,不會讓系統導航欄和我們app的UI重疊,導致交互問題。這樣說可能比較抽象,看看下面兩個效果圖的對比就知道了。


有fitsSystemWindows設置

沒有fitsSystemWindows設置

注:上面的演示效果,是藉助了我的另一個開源項目,詳情請戳:AndroidAlbum

這樣的話,如果我有10個Activity要實現這種效果,就要在10個佈局文件中做設置,非常麻煩。所以,想到一種方法,在theme中加上如下的android:fitsSystemWindows設置:

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

發現果真可以了。所有要實現 Translucent System Bar 的Activity,只需要設置了這個theme即可,改起來也很方便。可惜,後來出現了一個BUG,讓我還是得老老實實的回去佈局文件中設置。


Toast文字錯位

Toast打印出來的文字都往上偏移了。這裏也是我疏忽的地方,因爲在佈局文件中設置是對View生效,而到了theme進行設置則是對Window生效了,兩者在實現上就不一樣了。所以,最終只能改回原來的方式去實現。

實踐總結

最後做一下小小的總結:

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

怎樣,介紹到這裏,你會使用 Translucent System Bar 了嗎?趕快到你的app中引入吧!

補充更新(2016-02-19)

一些熱心的網友反饋,在Android 4.4平臺上使用第二種方法失效。我立馬跑到Android4.4的真機運行一遍,果真出現下面的bug,頂部變成黑白漸變了。


4.4平臺上第二種方案出現的BUG

在此,先爲自己的疏忽向廣大讀者說聲抱歉。以後會最大程度的避免這種低級錯誤的產生。下面給出此Bug的修復方案:

第一步:去到 ColorTranslucentBarActivity 的佈局文件中,將佈局劃分成爲標題佈局內容佈局兩部分;

第二步:將 ColorTranslucentBarActivity 的根佈局顏色設置與標題佈局的顏色一致,並將內容佈局設置爲白色;

<?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/color_31c27c"
    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
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/white"
        android:orientation="vertical">

        <Button
            android:id="@+id/btn_show_toast"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Show a toast" />
    </LinearLayout>

</LinearLayout>

經過以上兩步,即可在 4.4 平臺上實現 Translucent System Bar 的效果 。最後附上修復bug後的效果圖一張。


Android 4.4平臺bug修復後的效果圖

補充更新(2016-02-22)

很多童鞋反應,在每個佈局文件中都要寫上 android:fitsSystemWindows="true" ,有沒有更佳方便的方法,本人當時沒有思路。今天收到coder_sharp 童鞋反饋的一種更爲簡便的思路


coder_sharp童鞋提供的新思路

個人把他的思路,整理成代碼,如下:

public abstract class TranslucentBarBaseActivity 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();
}

所有需要實現效果的界面繼承以上的父類,並實現 getLayoutResId 抽象方法即可,就可以不用在佈局文件中不斷做重複操作了,具體代碼詳見工程中的 TranslucentBarBaseActivityBestTranslucentBarActivity

補充更新(2016-02-25)

近幾天在琢磨 Material Design 的一些新控件效果,意外的發現上面提到的第二種方式,在將原 values-v21/style.xml

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

</style>

換成

<style name="ColorTranslucentTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    ....
    .... 
    <item name="android:statusBarColor">@android:color/transparent</item>

</style>

之後,依舊可以實現同樣的效果。那麼,到了這裏你就可以發現,上面提到的兩種方式從本質上其實是殊途同歸(最終總結得到的就是一種方式)!

我是熱愛技術,喜歡開源和分享的Clock,很享受寫文和其他開發者們探討問題的樂趣,其中很多bug都是其他細心的開發者發現的。如果你對我寫的內容感興趣,歡迎關注我的簡書,我很樂意把我開發中一些有趣的東西分享到簡書中來(雖然目前僅僅是Android,但相信以後肯定會有其他內容的),希望與其他開發者共同探討,共同進步!

分享即美德,最後附上源代碼地址:
https://github.com/D-clock/AndroidSystemUiTraining



文/D_clock愛吃蔥花(簡書作者)
原文鏈接:http://www.jianshu.com/p/0acc12c29c1b
著作權歸作者所有,轉載請聯繫作者獲得授權,並標註“簡書作者”。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章