自從MD設(shè)計規(guī)范出來后,關(guān)于系統(tǒng)狀態(tài)欄的適配越受到關(guān)注,因為MD在5.0以后把系統(tǒng)狀態(tài)欄的顏色改為可由開發(fā)者配置的,而在5.0之前則無法指定狀態(tài)欄的顏色,所以這篇就說說使用Toolbar對系統(tǒng)狀態(tài)欄的適配策略
本文轉(zhuǎn)自:http://blog.csdn.net/u010687392/article/details/50684754
參考知乎:https://www.zhihu.com/question/31468556
主流App的適配效果
手Q在這方面適配非常好,將標(biāo)題欄和狀態(tài)欄合為一起了,和iOS效果一模一樣,如下:
4.4、5.0+

4.4以下版本
4.4以下版本則是系統(tǒng)默認(rèn)的黑色狀態(tài)欄,因為4.4以下沒辦法對狀態(tài)欄進(jìn)行改變和配置。
關(guān)于手Q的適配,對4.4以上所有版本都保留著一致的UI效果,即使在5.0以上,舍棄了MD風(fēng)格的狀態(tài)欄效果。
而微信的狀態(tài)欄則是對5.0以上開始適配MD風(fēng)格了,但是對5.0以下狀態(tài)欄則沒進(jìn)行適配,狀態(tài)欄還是系統(tǒng)默認(rèn)的黑色,下面是5.0+系統(tǒng)上的效果:

使用Toolbar進(jìn)行全版本適配
根據(jù)上面適配效果,我們可以這樣適配:
4.4以下系統(tǒng)則使用系統(tǒng)默認(rèn)的狀態(tài)欄,在4.4系統(tǒng)上將狀態(tài)欄和標(biāo)題欄合為一起了,在5.0以上系統(tǒng)有兩種選擇:
1.繼續(xù)使用4.4上的效果,和手Q一樣
2.適配MD風(fēng)格
如下是這兩種適配的效果:
第一種:
4.4以下系統(tǒng)上

4.4系統(tǒng)上:

5.0+系統(tǒng)上

第二種:
4.4以下系統(tǒng)上:

4.4系統(tǒng)上:

5.0+系統(tǒng)上

具體實現(xiàn)
第一種
主要就是在style文件中對4.4以上的設(shè)置透明狀態(tài)欄windowTranslucentStatus為true,或者也可以在代碼中設(shè)置,定義一個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設(shè)為:
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
這個至關(guān)重要,一般可以把Toolbar的布局為一個layout文件,以便后續(xù)復(fù)用。
1、values:
@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
false
true
2、values-v19:
false
true
true
3、values-v21:
false
true
true
@android:color/transparent
true
4、layout布局:
ImageView是RecyclerView頭部添加
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
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">
android:id="@+id/title"
style="@style/TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
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" />
第二種
大致和第一種一樣,差別就是values-v21的內(nèi)容了
1、values:
@color/colorPrimary@color/colorPrimaryDark@color/colorAccentfalsetrue
2、values-v19:
falsetruetrue
3、values-v21:
false
true
true
@color/colorPrimaryDark
4、layout布局:
和上面布局一樣
實現(xiàn)真正的全屏顯示,圖片在狀態(tài)欄下面
要實現(xiàn)圖片可以顯示在狀態(tài)欄下面,如上圖效果,則需要將狀態(tài)欄設(shè)為透明的,而第二種在5.0以上并沒有將狀態(tài)欄設(shè)為透明,所以你如果使用第一種適配方案,則直接使用即可,如果使用第二種適配發(fā)難,那么需要在相應(yīng)全屏的Activity中用代碼將狀態(tài)欄設(shè)為透明,因為values沒有設(shè)置,然后將
AppTheme.NoActionBar設(shè)為該全屏Activity的主題,畢竟全屏,那么就ok了
效果:

或者不加圖片,直接使用實現(xiàn)狀態(tài)欄和Toolbar合為一體效果:
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sunzxy.demoapplication.TestActivity">
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">
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" />
