【问题标题】:setTheme colorprimary not working as background in custom toolbar?setTheme colorprimary 不能作为自定义工具栏中的背景?
【发布时间】:2017-03-12 10:34:03
【问题描述】:

我刚刚添加了一个自定义工具栏。

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:background="@color/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark">

    </android.support.v7.widget.Toolbar>

请注意,我将背景设置为 colorPrimary。

在 style.xml 我有 2 种样式:

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

    <style name="Yellow" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#ffff00</item>
        <item name="colorPrimaryDark">#000000</item>
        <item name="colorAccent">#ffff00</item>
    </style>

我的颜色.xml

<resources>
    <color name="colorPrimary">#000</color>
    <color name="colorPrimaryDark">#fff</color>
    <color name="colorAccent">#000</color>
</resources>

我使用 setTheme 更改活动的颜色:

setTheme(colorId);

它可以工作,除了总是黑色的背景颜色,即使我将 setTheme 设置为黄色。

任何想法为什么?

【问题讨论】:

    标签: android xml android-layout android-studio android-xml


    【解决方案1】:

    下面的代码覆盖了工具栏的背景颜色

    android:theme="@style/ThemeOverlay.AppCompat.Dark" 
    

    。 从 Toolbar 中删除 android:theme 属性然后它就可以工作了。

    要更改文本颜色,请按照以下代码:

    <style name="CustomTextColor" parent="ThemeOverlay.AppCompat.Light">
        <item name="android:textColor">Your Text Color</item>
    </style>
    

    现在为您的工具栏设置主题:

    app:theme="@style/CustomTextColor"
    

    希望这会有所帮助。

    【讨论】:

    • 非常感谢!但是删除这个我的工具栏文本将是黑色的,我怎么能把它设置为白色?
    • 您可以将自定义主题添加到您的工具栏,您可以在其中定义工具栏文本颜色。如果你愿意,我会稍微添加代码。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-06
    • 2017-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多