Android沉浸式状态栏

2017-04-23  本文已影响0人  yaannis

效果图:

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文件设置:

<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>

说明:由于我们使用toolbar代替actionbar,所以先将style中的主体改为NoActionBar。
沉浸式状态栏智能从Android4.4(API版本为19)开始兼容,所以当版本高于19时,通过设置<item name="android:windowTranslucentStatus">true</item>使得状态栏透明,再将toolbar加长约25dp,设为75dp,使其延伸到状态栏并不遮挡状态栏本身的信息显示。版本低于19时,不会延伸至状态栏,toolbar则设为正常高度约50dp。(也可以不改变toolbar的高度,通过增加一个25dp的View来实现,颜色与toolbar一样)

上一篇下一篇

猜你喜欢

热点阅读