您的位置:首页 > 移动开发 > Android开发

android 沉浸式状态栏

2015-07-19 19:32 519 查看
针对5.0(api21)

首先定义主题

<resources>

    <!-- Base application theme. -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- tool_bar(actionbar)颜色 -->
        <item name="colorPrimary">#4876FF</item>
        <!-- 状态栏颜色 -->
        <item name="colorPrimaryDark">#4876FF</item>
        <!-- 窗口的背景颜色 -->
        <item name="android:windowBackground">@android:color/white</item>

        <item name="colorAccent">#ffff5722</item>
    </style>

    <style name="AppTheme" parent="AppBaseTheme"></style>
</resources>


在values-v21的styles.xml中定义

<resources>

    <style name="AppTheme" parent="AppBaseTheme">

        <!-- 底部导航栏颜色 -->
        <item name="android:navigationBarColor">#4876FF</item>
        <item name="android:windowTranslucentNavigation">true</item>

        <!-- 沉浸式的statubar,先将statusBar设置为透明,然后Activity的根布局为DrawerLayout,设置布局DrawerLayout的属性android:fitsSystemWindows="true"
        将colorPrimary和colorPrimaryDark设置成一样,否则分割线很明显-->
        <item name="android:statusBarColor">@android:color/transparent</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:windowTranslucentStatus">true</item>
        <item name="android:windowContentTransitions">true</item>

    </style>

</resources>


然后在根布局文件中定义

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/abs_screen_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/screen_content_parent"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <include
            android:id="@+id/toolbar"
            layout="@layout/tool_bar" />

    </LinearLayout>

</android.support.v4.widget.DrawerLayout>


一定要使用DrawerLayout,以及使用属性android:fitsSystemWindows="true"

以上即可在5.0机器上实现沉浸式状态栏

使用DrawerLayout的原因分析

在DrawerLayout的构造函数中,有此判断获取状态栏的背景

if (ViewCompat.getFitsSystemWindows(this)) {
            IMPL.configureApplyInsets(this);
            mStatusBarBackground = IMPL.getDefaultStatusBarBackground(context);
        }


public static Drawable getDefaultStatusBarBackground(Context context) {
        final TypedArray a = context.obtainStyledAttributes(THEME_ATTRS);
        try {
            return a.getDrawable(0);
        } finally {
            a.recycle();
        }
    }


private static final int[] THEME_ATTRS = {
            android.R.attr.colorPrimaryDark
    };


和前面style中定义的主题对应,然后再onDraw方法中画出来

@Override
    public void onDraw(Canvas c) {
        super.onDraw(c);
        if (mDrawStatusBarBackground && mStatusBarBackground != null) {
            final int inset = IMPL.getTopInset(mLastInsets);
            if (inset > 0) {
                mStatusBarBackground.setBounds(0, 0, getWidth(), inset);
                mStatusBarBackground.draw(c);
            }
        }
    }


以上实现,局限性非常大,以后研究使用普通的View来实现,以及在4.4中的实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: