Android 中具有动态 ActionBar 颜色的半透明 StatusBar

Translucent StatusBar with dynamic ActionBar color in Android

我正在尝试实现一个半透明的状态栏(以便我的导航视图 BEHIND 状态栏)但仍然喜欢动态更改我的操作栏的颜色。因此,状态栏颜色需要更改为我的操作栏颜色的深色版本。

如果我将状态栏设置为透明,如许多消息来源所暗示的那样,我的 primary_dark 颜色将用作状态栏的背景。但是,由于我会在运行时更改操作栏颜色,因此 primary_dark 不一定是我的操作栏的深色。

如果我将状态栏设置为操作栏颜色,透明度就会消失。如果我将状态栏设置为操作栏颜色并添加透明度,状态栏看起来既不错误也不正确,我的重叠导航视图仍然不是很 'transparent' / 'colorful'.

Google 收件箱具有三种不同的颜色:收件箱(蓝色)、已延后(黄色)和完成(绿色)。

我该怎么做才能实现这种行为?

实际上,它很容易实现。

第一步 - 改变 Toolbar:

的高度
  • height更改为wrap_content

所以对我来说它看起来像这样:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

然后覆盖 v19 的资源:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
            <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
                    <!-- Style properties -->
                    ....
                    <!-- These properties are important:-->
                    <item name="android:windowTranslucentStatus">true</item>
                    <item name="windowActionBarOverlay">false</item>
                    <item name="android:windowActionBarOverlay">false</item>
                    <item name="android:fitsSystemWindows">false</item>
            </style>
    </resources>

然后,在 Activity 中,为 Toolbar 设置 padding:

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setPadding(0, getStatusBarHeight(), 0, 0);
    ......
    ......
    public int getStatusBarHeight() {
        int result = 0;

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                result = getResources().getDimensionPixelSize(resourceId);
            }
        }
        return result;
    }

实际上,差不多就是这样。现在,一旦我想更改工具栏的颜色,我就调用它:

    if (getSupportActionBar() != null) {
        getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.GREEN));
    }

activity的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:openDrawer="start">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="false"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

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

注意! 在 Kitkat 之前的 OS 版本中,状态栏保持不变,不透明。

我已将测试应用程序的源代码上传到我的保管箱 - feel free to check it out

希望对你有所帮助