【问题标题】:Unable to add attribute primary color in android drawable xml无法在 android drawable xml 中添加属性原色
【发布时间】:2017-01-06 04:55:11
【问题描述】:

我的主题定义为

<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimaryRed</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
        <item name="colorAccent">@color/colorAccentRed</item>
    </style>

在我的 XML 布局中,我正在做

<android.support.design.widget.AppBarLayout
        android:background="?attr/colorPrimary"
        android:id="@+id/topBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

每当我更改任何主题时,colorPrimary 都会发生变化

但是,如果我在 drawable 中添加了相同的内容,例如

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="?attr/colorPrimary" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
    <item>
        <shape android:shape="rectangle">
            <solid android:color="?attr/colorPrimary" />
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>

由于无法膨胀视图而崩溃,该视图的背景设置为@drawabe/xxxx

如何在我的 XML drawable 中定义主题颜色属性

【问题讨论】:

  • 请发布您的堆栈跟踪。
  • 你有没有android.view.InflateException: Binary XML file line... ex?
  • @Charuka 是同样的错误
  • 我们不能在中使用android:color,我们需要使用android:drawable来设置背景,
  • @Charuka 问题是如何定义可绘制的属性颜色,因为我正在更改主题并且每个主题都有单独的颜色。在布局 xml 中它可以工作,但在可绘制 xml 中它没有

标签: java android android-drawable android-styles


【解决方案1】:

只需替换...

android:color="?attr/colorPrimary"

到...

android:color="@color/colorPrimaryRed"

更新

无法在 API 21 以下的 xml drawable 中引用属性。 为了制作您的主题,您需要:

为每个主题创建一个 xml drawable。 使用@color 标签或#RGB 格式将所需的颜色直接包含到您的drawable 中。

在 attrs.xml 中为您的可绘制对象创建一个属性。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- Attributes must be lowercase as we want to use them for drawables -->
    <attr name="my_drawable" format="reference" />
</resources>

将你的drawable添加到你的theme.xml中。

<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
   <item name="my_drawable">@drawable/my_drawable</item>
</style>

使用您的属性在布局中引用您的可绘制对象。

<TextView android:background="?my_drawable" />

【讨论】:

  • 我不想那样做 :)
  • @Abi 你看到选择器 xml,这就是我想要改变的,布局 xml 已经可以正常工作了。
  • android:background="?attr/colorPrimary" 这个 WORKS, 这个不行
  • @MuhammadUmar 试试?colorPrimary?android:colorPrimary
【解决方案2】:

这似乎在 API 21+ 上受支持。在此之下,您无法引用 drawable 中的属性。 https://code.google.com/p/android/issues/detail?id=26251.

这可能有助于解决方法: How to reference style attributes from a drawable?

【讨论】:

【解决方案3】:

你可以试试

<solid android:color="@android:attr/colorPrimary"/>

而不是

<solid android:color="?attr/colorPrimary" />

【讨论】:

  • 不起作用,伙计。第一个显然是 21 岁以上,另一个是我正在做的事情并崩溃了
【解决方案4】:

你可以使用:-

<solid android:color="@android:color/holo_orange_dark"  />

【讨论】:

    猜你喜欢
    • 2019-02-17
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2013-09-02
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多