Android代码需要一个可绘制的'属性错误

时间:2015-09-28 18:27:48

标签: android

Android工作室中的

我收到此错误:

<item> tag requires a 'drawable' attribute or child tag defining a drawable

但我在Intellij上没有这个错误,我的风格是:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#000000" android:state_pressed="true" />
    <item android:color="#ededed" android:state_pressed="false" />
</selector>

我在小部件背景上使用它

1 个答案:

答案 0 :(得分:0)

根据:

Selector documentation

您需要将drawable定义为选择器的一部分:

  

<强>物品

     

定义一个drawable放置在图层drawable中,在a中   由其属性定义的位置。必须是选择器的孩子   元件。接受子位图元素。属性:

     

android:drawable可绘制资源。需要。参考drawable   资源。

black_bg.xml(在可绘制文件夹中)

<?xml version="1.0" encoding="utf-8"?>
   <shape xmlns:android="http://schemas.android.com/apk/res/android">

      <solid android:color="#0000"/>

  </shape>

grey_bg.xml(在drawable文件夹中)

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">

        <solid android:color="#ededed"/>

    </shape>

selector.xml(也在drawables文件夹中)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/black_bg" />
    <item android:drawable="@drawable/grey_bg" />
</selector>