【问题标题】:How to manage status bar color?如何管理状态栏颜色?
【发布时间】:2015-12-18 11:52:00
【问题描述】:

我是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"/>

【问题讨论】:

    标签: android toolbar statusbar


    【解决方案1】:

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

    工作的sn-p代码:

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

    【讨论】:

      【解决方案2】:

      要为状态栏设置自定义颜色,请在您的应用程序主题的样式中使用 android:statusBarColor 属性,例如

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

      【讨论】:

      • 但是如果我用不同的工具栏创建了另一个 Activity
      • 此设置将影响您在应用程序上的所有活动
      • 但第二个工具栏颜色不同
      【解决方案3】:

      你可以这样做:

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

      【讨论】:

      • 还有,请您告诉我如何在抽屉打开时管理状态栏的颜色。抽屉的颜色与主屏幕的颜色不同。
      猜你喜欢
      • 1970-01-01
      • 2021-11-05
      • 1970-01-01
      • 2019-11-25
      • 2015-11-20
      • 2011-08-13
      • 2021-06-04
      相关资源
      最近更新 更多