問題
如圖所示,標題欄和背景顏色不統一,整體視感不協調。
背景
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