如何管理状态栏颜色?

How to manage status bar color?

我是 android 初学者,我想创建 status barToolbar 相同,当 DrawerLayout 打开时 statusBar 再次更改为DrawerLayout,这个问题怎么解决。我已经设置了风格parent="Theme.AppCompat.Light.NoActionBar

<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ed9797"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/DrawerLayout">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="#e62e2e"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="0dp"
    app:contentInsetStart="0dp">
</android.support.v7.widget.Toolbar>
</RelativeLayout>

<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />


    <ListView
        android:background="#46dfcd"
        android:id="@+id/leftDrawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:text="Drawer page"
        android:layout_gravity="start"/>

要更改状态栏颜色,请使用 setStatusBarColor(int color).

工作代码片段:

Window window = activity.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(activity.getResources().getColor(R.color.example_color));

你可以这样做:

     @Override
     protected void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);
           if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                Window window = getWindow();
                window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
                window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
                window.setStatusBarColor(getResources().getColor(
                                R.color.your_color));
            }
           setContentView(R.layout.your_layout);

                   //Your implementation
     }

要为状态栏设置自定义颜色,请在您的应用程序主题上使用 android:statusBarColor 属性 styles.xml like

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your status bar here. -->
    <item name="android:statusBarColor">@color/red</item>

    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>