【问题标题】:Toolbar TitleTextColor not changing in dark mode工具栏 TitleTextColor 在暗模式下没有变化
【发布时间】:2020-07-03 08:26:02
【问题描述】:

我正在集成暗模式,我有折叠工具栏和工具栏,它们包含在 appbarlayout 中, 问题是当我在设置暗模式时尝试将工具栏标题颜色设置为白色时,它在亮模式下不起作用,颜色相应地显示,我几乎尝试了所有方法,但我不知道到底发生了什么,我有 color 和 color night 文件夹,我为两者设置了颜色并将其添加到我的工具栏,但它没有改变任何东西,如果有人能提供帮助,我将不胜感激,谢谢

  • 这是在折叠标题之前

  • 在我折叠要在工具栏中设置的标题后,它不会显示白色(这是我在深色模式下想要的工具栏标题颜色的颜色)

  • 这是我的 xml 代码

 <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".View.MealDetails">

        <com.google.android.material.appbar.AppBarLayout
            android:id="@+id/appbarlaout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <com.google.android.material.appbar.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/collapsingtoolbar"
                android:fitsSystemWindows="true"
                app:contentScrim="@color/toolbarcolor"
                app:titleEnabled="true"
                app:title="@{details.strMeal}"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <ImageView
                    android:layout_width="match_parent"
                    android:layout_height="190dp"
                    app:mealDetailsImg="@{details.strMealThumb}"
                    android:background="@drawable/noimg"
                    tools:ignore="ContentDescription" />
                    <com.google.android.material.appbar.MaterialToolbar
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        android:id="@+id/mealdetailstoolbar"
                        app:menu="@menu/addfavmenu"
                        app:navigationIcon="@drawable/arrow_back_black"
                        app:layout_collapseMode="pin" />

            </com.google.android.material.appbar.CollapsingToolbarLayout>

        </com.google.android.material.appbar.AppBarLayout>
  • 这是颜色文件夹
<resources>
    <color name="colorPrimary">#6200EE</color> 
    <color name="colorPrimaryDark">#000000</color> // changing status bar color 
    <color name="colorAccent">#000000</color> // changing icons color 
    <color name="textColorPrimary">#000000</color> // i m not sure if this is correct but i think it is for toolbar text ( i could be wrong )
    <color name="ratetextcolor">#000000</color> /// this is to change rate text color
    <color name="iconscolor">#000000</color> // this is for icons color
    <color name="faviconcolor">#d10d06</color> // i have an icon which i wanted to settle to red in light mode and blue in dark mod
    <color name="categoryareacolor">#000000</color> //textviews
    <color name="headerColor">#ffffff</color>
</resources>

  • 这是夜间文件夹
<resources>
        <color name="colorPrimary">#6200EE</color>
        <color name="colorPrimaryDark">#6200EE</color>
        <color name="colorAccent">#6200EE</color>
        <color name="textColorPrimary">#ffffff</color>
        <color name="ratetextcolor">#ffffff</color>
        <color name="iconscolor">#3266a8</color>
        <color name="faviconcolor">#3266a8</color>
        <color name="categoryareacolor">#000000</color>
</resources>

【问题讨论】:

  • 您使用的是哪个应用主题?
  • 你能告诉我valuesvalues-night文件夹中的colors.xml
  • 我进行了编辑,谢谢,我不确定我是否做错了什么,textColorPrimary 不是负责更改工具栏标题颜色的吗?
  • 那些xml文件中没有toolbarcolor,它在哪里?

标签: android android-toolbar android-theme android-collapsingtoolbarlayout


【解决方案1】:

你可以使用:

 <com.google.android.material.appbar.AppBarLayout
    ...>

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:fitsSystemWindows="true"
            app:contentScrim="@color/..."
            android:theme="@style/CollapsingToolbarLayout_Overlay"
            >

    

与:

<style name="CollapsingToolbarLayout_Overlay">
    <!-- Title text color -->
    <item name="android:textColorPrimary">@color/....</item>
    <!-- Toolbar up color -->
    <item name="colorControlNormal">@color/...</item>
</style>   

【讨论】:

  • 将那一行样式添加到工具栏后,它使工具栏在我折叠标题之前可见,颜色也没有改变,这是它的照片,这是一个它的图像,imgur.com/a/fGxvV5t
  • 我已将主题添加到我的折叠工具栏,但由于某种原因在深色模式下仍然没有显示白色,textColorPrimary 负责更改颜色,但我不知道为什么它不,谢谢
  • 我确实做到了,没有任何改变,因为折叠的工具栏文本颜色是相关的,所以我通过编程解决了这个问题,我创建了一个检查模式并相应更改颜色的函数,谢谢你的帮助伙伴表示赞赏
【解决方案2】:

由于 android 会根据文件夹 values(浅色主题)和 values-night(深色主题)中的主题选择正确的颜色,但在这些 xml 文件中没有 toolbarcolor,因此它总是从 @ 中选择它AppTheme的987654325@

所以我认为以下方法可以工作(如果不行,请告诉我)

添加到valuescolors.xml

&lt;color name="toolbarcolor"&gt;your day color&lt;/color&gt;

添加到values-nightcolors.xml

&lt;color name="toolbarcolor"&gt;your night color&lt;/color&gt;

你可以从styles.xml中删除toolbarcolor

【讨论】:

  • 我确实做到了,没有任何改变,因为折叠的工具栏文本颜色是相关的,所以我通过编程解决了这个问题,我创建了一个检查模式并相应更改颜色的函数,谢谢你的帮助伙伴表示赞赏
  • 您是否也尝试在colors.xml 中添加textColor 属性?你是说你将颜色添加到contentSrim
猜你喜欢
  • 2021-09-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-31
  • 2015-02-28
  • 2015-11-17
  • 1970-01-01
  • 1970-01-01
  • 2020-08-08
相关资源
最近更新 更多