【问题标题】:(colorPrimaryDark) status bar color not working on android v21?(colorPrimaryDark) 状态栏颜色在 android v21 上不起作用?
【发布时间】:2015-11-12 07:11:11
【问题描述】:

我在 style.xml 上添加了 colorPrimaryDark 但在状态栏 android v21 上没有影响颜色。

我有一个自定义工具栏,我在 style.xml 代码中没有使用操作栏主题

如果有任何解决方案请帮助我?

样式代码:-

 <style name="AppTheme" parent="AppTheme.Base">
    <!-- Customize your theme here. -->
</style>

<style name="AppTheme.Base" parent="Theme.AppCompat.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

<style name="myCustomToolbarTheme" parent="ThemeOverlay.AppCompat.Light">
    <item name="android:textColorSecondary">@color/colorAccent</item>
</style>

风格 v21 :-

  <style name="AppTheme" parent="AppTheme.Base">
    <item name="android:colorPrimary">@color/colorPrimary</item>
    <item name="android:colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:colorAccent">@color/colorAccent</item>

    <item name="windowActionBar">true</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowDrawsSystemBarBackgrounds">true</item>
    <item name="android:statusBarColor">@android:color/transparent</item>
</style>

工具栏代码:-

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
android:elevation="4dp"
android:background="@color/colorPrimary"
android:popupTheme="@style/Base.ThemeOverlay.AppCompat.Light"
xmlns:android="http://schemas.android.com/apk/res/android" />

主布局代码

<android.support.design.widget.CoordinatorLayout 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">

<LinearLayout
    android:fitsSystemWindows="true"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical">

    <include layout="@layout/custom_toolbar"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button1" />

</LinearLayout>


<android.support.design.widget.FloatingActionButton
    xmlns:fab="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/fab"
    android:layout_gravity="right|bottom"
    android:src="@mipmap/ic_plus"
    android:onClick="fabClick"
    android:layout_marginRight="@dimen/fab_margin"
    android:layout_marginBottom="@dimen/fab_margin_bottom"
    fab:borderWidth="0dp"/>

【问题讨论】:

    标签: android


    【解决方案1】:

    我认为您的 CoordinatorLayout 缺少以下属性:android:fitsSystemWindows="true"

    <android.support.design.widget.CoordinatorLayout
        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:fitsSystemWindows="true">
    

    【讨论】:

    • 感谢上帝。你刚刚拯救了我的一天。 拳头撞击
    • 我想我在做一些布局更改时不小心删除了这个属性。过去几个小时一直在寻找这个解决方案。
    【解决方案2】:

    很久以前,但我还是会回答,你应该删除这一行:

    <item name="android:statusBarColor">@android:color/transparent</item>
    

    它覆盖了colorPrimaryDark 颜色并基本上将其从状态栏中删除。

    【讨论】:

    • 它不是压倒一切的。如果你添加 android:statusBarColor 那么它不会覆盖 colorPrimaryDark。
    • 如果我想覆盖 colorPrimaryDark 怎么办?
    【解决方案3】:

    在 values-v21 中创建一个新的 styles.xml 文件,并在 AppTheme 中添加以下属性。

    <item name="android:statusBarColor">@color/colorPrimaryDark</item>
    

    我在 styles.xml (v21) 中关注 AppTheme

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

    【讨论】:

    • 虽然此解决方案有效,但它会覆盖状态栏颜色。因此,如果您有一个抽屉式导航或 CollapsingToolbarLayout,您将在状态栏后面有一个纯色栏而不是图像。
    【解决方案4】:

    尝试使用

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        getWindow().setStatusBarColor(getResources().getColor(R.color.colorPrimaryDark));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-12
      • 2017-03-05
      • 1970-01-01
      相关资源
      最近更新 更多