Android最佳實踐之SystemBar狀態欄全版本適配方案

fuoxtfjf 8年前發布 | 64K 次閱讀 Android開發 移動開發

來自: http://blog.csdn.net/u010687392/article/details/50684754


前言

自從MD設計規范出來后,關于系統狀態欄的適配越受到關注,因為MD在5.0以后把系統狀態欄的顏色改為可由開發者配置的,而在5.0之前則無法指定狀態欄的顏色,所以這篇就說說使用Toolbar對系統狀態欄的適配策略

主流App的適配效果

手Q在這方面適配非常好,將標題欄和狀態欄合為一起了,和iOS效果一模一樣,如下:

4.4、5.0+

這里寫圖片描述

4.4以下版本

4.4以下版本則是系統默認的黑色狀態欄,因為4.4以下沒辦法對狀態欄進行改變和配置。

關于手Q的適配,對4.4以上所有版本都保留著一致的UI效果,即使在5.0以上,舍棄了MD風格的狀態欄效果。
而微信的狀態欄則是對5.0以上開始適配MD風格了,但是對5.0以下狀態欄則沒進行適配,狀態欄還是系統默認的黑色,下面是5.0+系統上的效果:
這里寫圖片描述

使用Toolbar進行全版本適配

根據上面適配效果,我們可以這樣適配:

4.4以下系統則使用系統默認的狀態欄,在4.4系統上將狀態欄和標題欄合為一起了,在5.0以上系統有兩種選擇:
1.繼續使用4.4上的效果,和手Q一樣
2.適配MD風格

如下是這兩種適配的效果:

第一種:

4.4以下系統上

這里寫圖片描述

4.4系統上:

這里寫圖片描述

5.0+系統上

這里寫圖片描述

第二種:

4.4以下系統上:

這里寫圖片描述

4.4系統上:

這里寫圖片描述

5.0+系統上

這里寫圖片描述

具體實現

第一種

主要就是在style文件中對4.4以上的設置透明狀態欄windowTranslucentStatus為true,或者也可以在代碼中設置,定義一個BaseActivity:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // Translucent status bar
            getWindow().setFlags(
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
                    WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }

然后在布局文件中對Toolbar設為:

android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"

這個至關重要,一般可以把Toolbar的布局為一個layout文件,以便后續復用。

1、values:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
         <!-- Customize your theme here. -->
    </style>

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style></pre> 

2、values-v19:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

3、values-v21:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

4、layout布局:

ImageView是RecyclerView頭部添加

   <android.support.design.widget.CoordinatorLayout xmlns:android="

<android.support.v7.widget.RecyclerView
    android:id="@+id/recycle_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:id="@+id/title"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/img"
        android:layout_width="48dp"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="right"
        android:clickable="true"
        android:scaleType="centerInside"
        android:src="@android:drawable/stat_sys_headset" />
</android.support.v7.widget.Toolbar>

</android.support.design.widget.CoordinatorLayout></pre>

第二種

大致和第一種一樣,差別就是values-v21的內容了

1、values:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
         <!-- Customize your theme here. -->     </style>

<style <span class="hljs-property">name</span>=<span class="hljs-string">"AppTheme.NoActionBar"</span>>
    <<span class="hljs-property">item</span> <span class="hljs-property">name</span>=<span class="hljs-string">"windowActionBar"</span>><span class="hljs-constant">false</span></<span class="hljs-property">item</span>>
    <<span class="hljs-property">item</span> <span class="hljs-property">name</span>=<span class="hljs-string">"windowNoTitle"</span>><span class="hljs-constant">true</span></<span class="hljs-property">item</span>>
</style></code></pre> 

2、values-v19:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowTranslucentStatus">true</item>
    </style>

3、values-v21:

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>

4、layout布局:

和上面布局一樣

實現真正的全屏顯示,圖片在狀態欄下面

要實現圖片可以顯示在狀態欄下面,如上圖效果,則需要將狀態欄設為透明的,而第二種在5.0以上并沒有將狀態欄設為透明,所以你如果使用第一種適配方案,則直接使用即可,如果使用第二種適配發難,那么需要在相應全屏的Activity中用代碼將狀態欄設為透明,因為values沒有設置,然后將
AppTheme.NoActionBar設為該全屏Activity的主題,畢竟全屏,那么就ok了

<RelativeLayout xmlns:android="

<ImageView  android:id="@+id/img" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" android:src="@mipmap/bg" />

</RelativeLayout></pre>

效果:
這里寫圖片描述

或者不加圖片,直接使用實現狀態欄和Toolbar合為一體效果:

<RelativeLayout xmlns:android="

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    android:minHeight="?attr/actionBarSize"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:id="@+id/title"
        style="@style/TextAppearance.AppCompat.Subhead"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"
        android:textSize="20sp" />
</android.support.v7.widget.Toolbar>

</RelativeLayout></pre>

這里寫圖片描述

</div>

 本文由用戶 fuoxtfjf 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!