【问题标题】:Toolbar and statusbar color doesn't match工具栏和状态栏颜色不匹配
【发布时间】:2019-10-23 14:42:54
【问题描述】:

状态栏和工具栏的显示方式如下:

工具栏的颜色必须延伸到状态栏的颜色,但我不明白这是怎么回事。

这是xml 的活动文件:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".view.AmministratoreActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar_amministratore"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:elevation="4dp"/>

</RelativeLayout>

在清单中,我对整个应用使用 android:theme="@style/AppTheme.NoActionBar

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
        <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    </style>


</resources>

【问题讨论】:

  • 清单中的主题是什么?你应该使用 android:theme="@style/AppTheme" tools:replace="android:theme"

标签: android android-toolbar android-statusbar


【解决方案1】:

将此代码放在 onCreate 方法中的 setContentView() 上方

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    Window window = getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
    window.setStatusBarColor(Color.BLUE);
 }

【讨论】:

    【解决方案2】:

    在 onCreate() 方法 @Russo 中的 SetContentView() 下面试试这个

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            // clear FLAG_TRANSLUCENT_STATUS flag:
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
       window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);     
       window.setStatusBarColor(getResources().getColor(R.color.your_desired_color));
    
        }
    

    【讨论】:

      【解决方案3】:

      public abstract void setStatusBarColor (int color)

      上述方法在 API 级别 21 中添加。因此您可以使用下面的代码 sn-p 以编程方式更改状态栏颜色。

      Window window = activity.getWindow();
      
      // clear FLAG_TRANSLUCENT_STATUS flag:
      window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
      
      // add FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS flag to the window
      window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
      
      // finally change the color
      window.setStatusBarColor(ContextCompat.getColor(activity,R.color.my_statusbar_color));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-26
        • 1970-01-01
        • 2015-09-15
        • 2015-02-22
        • 1970-01-01
        • 2015-02-28
        相关资源
        最近更新 更多