2016-09-19 20 views
2

当我尝试在自定义drawable xml中放置一个attr时,我遇到了一个问题。如何将attr放入自定义drawable中?

这里是我的布局的样本:

 <Button 
      android:id="@+id/btn" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:background="@drawable/my_custom_btn"/> 

然后我的自定义绘制 “my_custom_btn”,我想与attr_my_custom_color指定其颜色:

<?xml version="1.0" encoding="utf-8"?> 
    <selector xmlns:android="http://schemas.android.com/apk/res/android"> 
     <item android:state_pressed="true" 
      android:drawable="?attr/attr_my_custom_color" /> <!-- pressed --> 
    </selector> 

我attr.xml文件:

<resources> 
    <attr name="attr_my_custom_color" format="color"/> 
</resources> 

而我的style.xml文件,我的appli阳离子使用此主题(在我的清单):当我在我的布局测试

<resources> 
<style name="AppThemeTest" parent="Theme.AppCompat.Light.NoActionBar"> 

    <item name="attr_my_custom_color">@color/primary</item> 

</style> 

我ATTR工作正常,但是当我把它放在我的自定义绘制里面,这是行不通的。

它编译罚款,但是当我启动我的应用程序,它会显示这个错误:

造成的:org.xmlpull.v1.XmlPullParserException:二进制XML文件行#4:标签需要“绘制”属性或子标签定义一个可绘制的

你能帮我指定一个自定义drawable里面的attr吗?

回答

0
you have to change background to drawable 


<Button 
     android:id="@+id/btn" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawable ="@drawable/my_custom_btn"/> 
+0

它不会崩溃了,但它并不适用'<项目的android:state_pressed = “真” 机器人:可绘制= “?ATTR/attr_my_custom_color”/><! - 按 - > “要么。 – curiousStranger123

+0

你的minSdkVersion是什么? –

+0

minSdkVersion 16 – curiousStranger123

相关问题