Android Selector Drawable不适用于属性

时间:2015-06-10 12:43:50

标签: android drawable selector-drawable

我正在使用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文件中定义的颜色替换属性,那同样的drawable工作。

使用颜色绘制样本:

<?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>

提前致谢!

4 个答案:

答案 0 :(得分:7)

最后,发现了问题。 android [pre-lollipop]操作系统中有一个错误,它不允许你在drawable中使用attr。这是bug的链接:

  

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

Android开发团队发布了一个修复程序,但它适用于Android L及更高版本。有关此问题的解决方法,请参阅以下解决方案:

  

How to reference style attributes from a drawable?

答案 1 :(得分:1)

我不知道如果它仍然与你相关,但我在同样的事情上挣扎,我想我找到了一个解决方法(有点)。直接使用?attr /不起作用(在api 21及更高版本上工作,但即便如此,它也不适用于颜色选择器(仅适用于drawable)。

所以我这样做了(给你我自己的例子)。 在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) {  }

我希望这会有所帮助: - )

答案 2 :(得分:1)

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

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

为说明您的情况,解决方案将改为:

<?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>

希望有帮助!

答案 3 :(得分:-6)

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