Android Selector Drawable 不适用于属性

Android Selector Drawable doesnt work with attributes

我正在使用 attr 为我的项目创建一个可绘制的选择器,这样一旦我更改主题颜色,我就不必在可绘制文件中进行任何更改。我正在使用以下库:

compile 'com.android.support:appcompat-v7:+'
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:design:22.2.0'

这里是 drawable 的源代码:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
    <item android:drawable="?attr/colorPrimary"/>
</selector>

在同一代码中,如果我用 colors.xml 文件中定义的颜色替换属性,则相同的可绘制对象可以工作。

带有颜色的可绘制示例:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/color_primary" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@color/color_primary_dark" android:state_pressed="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
    <item android:drawable="@color/color_primary"/>
</selector>

提前致谢!

而不是放一个“?”将其更改为“@”

终于找到问题了。 android [pre-lollipop] OS 中存在一个错误,它不允许您在 drawable 中使用 attr。这是 link 错误:

https://code.google.com/p/android/issues/detail?id=26251

Android 开发团队已发布修复程序,但它适用于 android L 和 above.For 解决此问题的方法,请参考以下解决方案:

How to reference style attributes from a drawable?

我不知道它是否仍然与您相关,但我也在为同样的事情苦苦挣扎,我想我已经找到了解决方法(有点)。直接使用 ?attr/ 不起作用(适用于 api 21 及更高版本,但即便如此它也不适用于颜色选择器(仅适用于可绘制对象)。

所以我是这样做的(给你我自己的例子)。 在 attr.xml

中创建属性
<attr name="nav_item_color_selector" format="reference|color"/>

然后在您使用的所有主题中,像这样添加属性(f.e。对于 Light 主题):

<item name="nav_item_color_selector">@color/text_color_selector_light</item>

或深色主题(默认):

<item name="nav_item_color_selector">@color/text_color_selector</item>

现在我的 text_color_selector.xml(两者)看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true"
      android:color="@color/myAccentColor" />
<item android:state_selected="false" 
      android:color = "@color/myTextPrimaryColor"/>
<item android:color="@color/myTextPrimaryColor" />

当我想使用它们时,f.e 在为我的自定义图像视图着色时,我使用:

    <com.yourdomain.yourpackage.NavDrawerIcon
    android:layout_width="24dp"
    android:layout_height="24dp"
    android:layout_gravity="center_vertical"
    android:src="@mipmap/ic_launcher"
    android:id="@+id/nav_item_icon"
    android:layout_margin="24dp"
    android:tint = "?attr/nav_item_color_selector"
    android:tintMode="src_atop"/>

您也可以使用 TypedValue 以编程方式重新获取它,如下所示:

    TypedValue typedValue = new TypedValue();
    Resources.Theme theme = context.getTheme();
    theme.resolveAttribute(R.attr.nav_item_color_selector, typedValue, true);
    XmlResourceParser parser = viewGroup.getContext().getResources().getXml(typedValue.resourceId);
    try {
        ColorStateList sl = ColorStateList.createFromXml(viewGroup.getContext().getResources(), parser);
        viewHolder.textView.setTextColor(sl);

    } catch (Exception e) {  }

希望对您有所帮助:-)

我遇到了同样的问题,截至 2019 年,该问题尚未解决,因此您无法在选择器中将属性引用为可绘制对象。我将分享我为该问题找到的解决方案,因为我没有在此处看到它。我在 bug report 的最后一条评论中找到了它,mudit 在他的回答中也提到了它。

解决方法基本上是创建一个引用属性值的可绘制资源。

为了说明您的情况,解决方案应该是:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="?attr/colorPrimary" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="?attr/colorPrimaryDark" android:state_pressed="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
    <item android:drawable="?attr/colorPrimary"/>
</selector>

您将替换可绘制资源的 ?attr/*:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/colorPrimaryDrawable" android:state_enabled="true" android:state_window_focused="false"/>
    <item android:drawable="@drawable/colorPrimaryDarkDrawable" android:state_pressed="true"/>
    <item android:drawable="@android:color/darker_gray" android:state_enabled="false"/>
    <item android:drawable="@drawable/colorPrimaryDrawable"/>
</selector>

可绘制对象将定义为:

drawable/colorPrimaryDrawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
    <solid android:color="?attr/colorPrimary" />
</shape>

drawable/colorPrimaryDarkDrawable

<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
    <solid android:color="?attr/colorPrimaryDark" />
</shape>

希望对您有所帮助!!

我 运行 在可绘制选择器中使用颜色属性时遇到了类似的问题。

我通过制作 颜色选择器 而不是 可绘制选择器 解决了这个问题。 基本上,创建一个新资源,资源类型为颜色,这将是您的颜色选择器。在这里,与可绘制选择器不同,您可以毫无问题地使用您的属性:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="?colorBackgroundDisabled" android:state_enabled="false" />
    <item android:color="?colorBackground" />
</selector>

^我把这个命名为selector_background。

然后你可以使用那个选择器:

<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/selector_background"/>