Android 狀態欄背景設置

這裏寫圖片描述

問題

如圖所示,標題欄和背景顏色不統一,整體視感不協調。

背景


1.Android 4.4之前狀態欄默認黑色,不能改變
2.Android 4.4可以設置狀態欄是否透明
3.Android 5.0+可以主動設置狀態欄背景

預期效果

狀態欄和toolbar或背景色一致,如:

這裏寫圖片描述

這裏寫圖片描述

初步方案

1.Android 4.4以下,不做處理
2.Android 4.4+ ,設置狀態欄透明

實現

style

創建values、values-v19、values-v21三個目錄,分別在三個目錄下創建styles.xml文件,在該文件中創建相同主題,暫且取名叫TranslucentSystemBar吧。

values

 <style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar"/>

values-v19

<style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar">
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
    </style>

values-v21

 <style name="TranslucentSystemBar" parent="@style/AppTheme.NoActionBar" >
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowTranslucentNavigation">true</item>
        <!--Android 5.x 開始需要把狀態欄顏色設爲透明,否則會呈現系統默認的灰色-->
        <item name="android:statusBarColor">@android:color/transparent</item>
    </style>

android:fitsSystemWindows="true"

要在xml文件中添加

android:fitsSystemWindows="true"

屬性,該屬性的作用是佈局時,忽略狀態欄高度對整體佈局的影響。如果屬性設置爲false的話,即

android:fitsSystemWindows="false"

效果如下:
這裏寫圖片描述

參考如下:

http://www.jianshu.com/p/0acc12c29c1b
http://www.jianshu.com/p/140be70b84cd?utm_source=tuicool&utm_medium=referral

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章