效果圖:
Screenshot.png
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/toolbar_height"
android:background="@color/mainColor"
android:fitsSystemWindows="true"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
styles滞诺、styles-v19文件設(shè)置:
<resources>
<!-- Base application theme. -->
<style name="BaseAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/mainColor</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme" parent="@style/BaseAppTheme">
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="@style/BaseAppTheme">
<item name="android:windowTranslucentStatus">true</item>
</style>
</resources>
dimens、dimens-v19文件分別配置toolbar_height:
<dimen name="toolbar_height">50dp</dimen>
<dimen name="toolbar_height">75dp</dimen>
說(shuō)明:由于我們使用toolbar代替actionbar,所以先將style中的主體改為NoActionBar褐啡。
沉浸式狀態(tài)欄智能從Android4.4(API版本為19)開(kāi)始兼容继控,所以當(dāng)版本高于19時(shí)斟薇,通過(guò)設(shè)置<item name="android:windowTranslucentStatus">true</item>使得狀態(tài)欄透明,再將toolbar加長(zhǎng)約25dp耍攘,設(shè)為75dp榕栏,使其延伸到狀態(tài)欄并不遮擋狀態(tài)欄本身的信息顯示。版本低于19時(shí)少漆,不會(huì)延伸至狀態(tài)欄臼膏,toolbar則設(shè)為正常高度約50dp。(也可以不改變toolbar的高度示损,通過(guò)增加一個(gè)25dp的View來(lái)實(shí)現(xiàn)渗磅,顏色與toolbar一樣)