前言
android狀態(tài)欄,如圖委煤,是頂部顯示時間電量那一部分堂油。根據(jù)Google MD設計規(guī)范是要讓狀態(tài)欄的顏色略深于標題欄。之前的狀態(tài)欄都是系統(tǒng)默認的黑色碧绞,為了美觀在5.0系統(tǒng)提出了沉浸式狀態(tài)欄府框,而關于沉浸式的效果其實可以向下兼容到4.4系統(tǒng)上。我這里提出自己在公司用的一種real簡單的方案讥邻。
思路
首先介紹android:windowTranslucentStatus
這一屬性迫靖,設置為true則狀態(tài)欄變透明(4.4以上手機),此時不做任何處理會出現(xiàn)如圖的效果:
可以看出標題欄將狀態(tài)欄覆蓋兴使,很不優(yōu)雅系宜。
接下來介紹android:fitsSystemWindows
這一屬性。設置為true讓Activity 中setContentView
的布局不覆蓋狀態(tài)欄(即相當于給狀態(tài)欄設置了padding)鲫惶,這個屬性要在根布局中使用蜈首,如果同時設置了
<item name="android:windowTranslucentStatus">true</item>
android:fitsSystemWindows="true"
你將看到如下效果实抡。
狀態(tài)欄透明欠母,標題欄不覆蓋狀態(tài)欄.
如果不考慮兼容4.4系統(tǒng)的化可以在5.0以上系統(tǒng)可以直接通過styles.xml設置
<item name="android:statusBarColor">@color/colorPrimaryDark</item>
(代碼設置setStatusBarColor())來為你的狀態(tài)欄填充顏色欢策。
如果要兼容4.4系統(tǒng)可以通過設置
<item name="android:windowTranslucentStatus">true</item>
讓狀態(tài)欄透明。然后在狀態(tài)欄放一個view赏淌,長在4.4以上為25dp踩寇,以下為0dp,寬為match_parent,背景色設置為colorPrimaryDark(或者其他你想狀態(tài)欄顯示的顏色)六水。
效果如下:
具體方案
自己寫一個標題欄的layout布局(以后用都可以include引用)俺孙,在該布局中添加如下代碼
<View android:layout_width="match_parent"
android:background="@color/colorPrimaryDark"
android:layout_height="@dimen/toolbar_padding_top"></View>
這個用來填充狀態(tài)欄,如果想設置半透明等效果可以放張ImageView圖片掷贾。
高度引用在4.4以上為25dp,以下為0dp睛榄。
所以只需要在res下新建一個values-v19包添加styles和dimens兩個xml文件
//styles.xml里加上
<item name="android:windowTranslucentStatus">true</item>
//dimens里加上
<dimen name="toolbar_padding_top">25dp</dimen>
然后在需要標題欄的地方include標題欄布局文件,不需要的地方通過
android:windowTranslucentStatus
和android:fitsSystemWindows
兩個屬性設置即可以滿足日常開發(fā)需求想帅。
其他
關于DrawerLayout和CoordinatorLayout目前我使用沒什么問題场靴,如果各位有什么疑惑可以提出來。
最后如果你懶得寫標題欄布局文件我這有個現(xiàn)在用的港准,點擊下載密碼:81ax