【问题标题】:Setting theme colors in drawable pre-lollipop?在可绘制的棒棒糖中设置主题颜色?
【发布时间】:2018-09-19 06:35:28
【问题描述】:

我一直在我的所有可绘制对象中使用主题属性,但我发现这在 21 年前不受支持

有什么方法可以使用

<shape android:shape="rectangle"
xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="?attr/background_1" />
<corners android:radius="@dimen/button_corner_radius" />

我的布局中有这样的可绘制对象吗?非常感谢。不知道现在该怎么办

【问题讨论】:

    标签: android


    【解决方案1】:

    您不能从 xml 文件中执行此操作。正如您在answer 中看到的那样,您必须创建与要使用的背景颜色一样多的不同资源文件。在您的情况下,如果我们将您的文件称为 shape_drawable.xml:

    在styles.xml中

    <attr name="shape_drawable" format="reference"/>
    

    然后在实际的主题中:

    // Theme X
    <item name="shape_drawable">@drawable/shape_drawable_x</item>
    // Theme Y
    <item name="shape_drawable">@drawable/shape_drawable_y</item>
    

    其中 shape_drawable_x 将使用纯色 x,而 shape_drawable_y 将使用纯色 y。

    然后,在你实际使用drawable的地方:

    android:src="?attr/shape_drawable"
    

    【讨论】:

    • 谢谢,我想通了,但我忘了关闭它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2018-10-08
    • 2015-08-14
    • 2017-10-16
    • 1970-01-01
    • 2015-03-06
    相关资源
    最近更新 更多