Android选择器Drawable不适用于属性
我正在使用attr为我的项目创建一个可绘制的选择器,这样一旦我更改了主题颜色,我就不必对可绘制文件进行任何更改。我正在使用以下LIB:Android选择器Drawable不适用于属性,android,drawable,selector-drawable,Android,Drawable,Selector Drawable,我正在使用attr为我的项目创建一个可绘制的选择器,这样一旦我更改了主题颜色,我就不必对可绘制文件进行任何更改。我正在使用以下LIB: 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
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]操作系统中存在一个漏洞,不允许您在drawable中使用attr。以下是bug的链接: Android开发团队已经发布了一个修复程序,但它适用于Android L及以上版本。有关此问题的解决方法,请参阅以下解决方案:
我不知道这是否仍然与你有关,但我一直在努力解决同样的问题,我想我已经找到了解决办法(有点)。直接使用?attr/不起作用(适用于api 21及更高版本,但即使如此,它也不适用于颜色选择器(仅适用于绘图) 所以我就这样做了(给你们举个我自己的例子)。 在attr.xml中创建一个atribute
<attr name="nav_item_color_selector" format="reference|color"/>
我希望这有帮助:-)我遇到了同样的问题,到2019年还没有解决,因此您不能将选择器中的属性引用为可绘制属性。我将分享我为这个问题得到的解决方案,因为我没有在这里看到它。我在最后一段评论中发现了这一点,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>
可抽出物的定义如下:
可拉伸/彩色PrimaryDrawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimaryDark" />
</shape>
可绘制/彩色PrimaryDarkDrawable
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimaryDark" />
</shape>
希望能有帮助 它与@不起作用。你有什么特别的原因要我改变主意吗?对。通常,attr是通过?而不是@符号。绘图设备如何使用颜色???您必须通过drawable而不是colorandroid来处理该零件本身。。请参见第二个可绘制的代码。它起作用了。问题是当你使用属性时。我想你必须使用类似“谢谢你的帮助”的东西,但这个解决方案对我来说不可行,因为它需要我用java代码为每个控件设置背景。我理解。我不知道您需要实现什么,但是如果它是布局或视图的背景,也可以使用?attr引用您在xmL中创建的属性来设置它。不一定是java。即使是在java中,关键是你只需要做一次。因为一旦你得到了你使用的每个主题的属性,它就会读取引用并以友好的方式设置选择器。@daxgirl你有没有一个例子,在棒棒糖前的
attr
值中使用TypedValue和StateListDrawable
?
<?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>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
</shape>
<shape xmlns:android="http://schemas.android.com/apk/res/android"android:shape="rectangle">
<solid android:color="?attr/colorPrimaryDark" />
</shape>