1. 程式人生 > >Android中 4.4-5.0 系統狀態列顏色的修改。實現Translucent System Bar

Android中 4.4-5.0 系統狀態列顏色的修改。實現Translucent System Bar

在這裡我們使用一種簡單的方法,之前在網上也看過類似的實現方法,一種是直接在程式碼裡面去設定,還有一種直接修改主題的樣式,個人感覺後面一種比較方便,也容易理解,在這裡就介紹一下第二種方法。

首先要到AndroidManifest中為指定的Activity去設定Theme,不過我們不能在values/style.xml中直接去定義Translucet System Bar的Theme,因為對應的版本不一樣,該特性只相容Android 4.4開始的版本。可以建幾個不同版本的values,比如values、values-v19、values-v21。

利用主題樣式去設定的話。

1.我們先把values建立完成,裡面有三個版本的styles。


values/styles.xml

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"><!--Android 4.4之前的版本上執行,直接跟隨系統主題-->
  </style>

values-v19/styles.xml

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus"
>true</item> <item name="android:windowTranslucentNavigation">true</item> </style>
設定了屬性 windowTranslucentStatus 和windowTranslucentNavigation 為true,我們必須設定這兩個屬性的值為true才行,以便使我們定義的layout侵入系統欄的領域。

values-v21/style.xml

<style name="TranslucentTheme" parent="Theme.AppCompat.Light.NoActionBar"
> <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進行設定

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/TranslucentTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

3.在Activity的佈局檔案中把android:fitsSystemWindows設定為true

雖然不加android:fitsSystemWindows屬性也會改變狀態列的背景,但是出現的結果是不同的。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="wxt.example.com.as20.MainActivity"
android:background="#FF4081">
    <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
    <Button
android:onClick="open"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="開啟"/>
</LinearLayout>

這個是個LinearLayout佈局,我們把android:fitsSystemWindows屬性在裡面設定。如果background是一種顏色的話,他會在整個螢幕上都是這種顏色。 那麼我們把android:fitsSystemWindows屬性去掉之後呢,兩者有什麼差別呢?

加了android:fitsSystemWindows屬性的介面(左) 不加android:fitsSystemWindows屬性的介面(右)


在這裡我們先來介紹一下fitsSystemWindows,

fitsSystemWindows 是在 android 4.4 中引入的。 System windows 顧名思義就是系統視窗,系統在這裡顯示系統一些屬性和操作區域,比如 最上方的狀態列,以及沒有實體按鍵的最下方的虛擬導航欄。

大部分的時候,你的應用都不會在狀態列和導航欄下面顯示內容,如果你需要在他們下面顯示內容,則需要確保你應用的可互動控制元件(比如按鈕)不要顯示在系統視窗下面了。 android:fitsSystemWindows=“true” 預設行為就是通過在 View 上設定和系統視窗一樣高度的邊框(padding )來確保你的內容不會出現到系統視窗下面。

android:fitsSystemWindows屬性,我們可以根據自己的需要去設定,並不一樣要設定在第一個跟佈局的屬性裡,我們來看一下另外一種

<?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/about_bg_darker"
android:orientation="vertical">
    <LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/security_setting_title_bg"
android:fitsSystemWindows="true"
android:gravity="center_vertical"
android:orientation="horizontal">
        <ImageButton
android:id="@+id/common_title_back_btn"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar_indicator_bg2"
android:contentDescription="@null"
android:src="@drawable/actionbar_back" />
        <TextView
android:id="@+id/common_title_main_text"
style="@style/common_title_main_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_feedback_setting" />
        <View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
        <ImageButton
android:id="@+id/common_title_main_extra_btn"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:background="@drawable/actionbar_indicator_bg2"
android:contentDescription="@null"
android:src="@drawable/submit_setting_feedback" />
    </LinearLayout>
    <LinearLayout
android:id="@+id/setting_menu"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:background="@drawable/bg_card3"
android:gravity="center_vertical"
>
        <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="7"
android:gravity="center_vertical">
            <TextView
android:id="@+id/setting_feedback_menu_select"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:textColor="#5d9cec"
android:textSize="14dp"
android:text="@string/activity_setting_feedback_common"/>
        </RelativeLayout>
        <RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="3"
android:gravity="center_vertical|right">
            <ImageView
android:id="@+id/menu_imageview"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_marginRight="12dp"
android:src="@drawable/arrow_07"/>
        </RelativeLayout>
    </LinearLayout>
    <EditText
android:id="@+id/container_setting_feedback"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="@id/setting_feedback_menu"
android:layout_marginTop="10dp"
android:background="#FFFFFF"
android:fontFamily="@string/font_families_roboto"
android:gravity="top"
android:hint="@string/container_hint_setting_feedback"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="15dp"
android:textColor="#787878"
android:textColorHint="#B5B5B5"
android:textSize="16dp" />
    <TextView
android:id="@+id/notice_setting_feedback"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/container_setting_feedback"
android:fontFamily="@string/font_families_roboto"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="10dp"
android:text="@string/notice_setting_feedback"
android:textSize="12dp" />
</LinearLayout>


在這裡我們把android:fitsSystemWindows屬性設定到第二個LinearLayout中了,第二個LinearLayout是類似於actionbar的佈局。我們來看一下效果圖: