Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable

前端之家收集整理的这篇文章主要介绍了Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

在LinearLayout中利用selector实现按钮点击效果的时候,发现错误如下:

Binary XML file line #4: <item> tag requires a 'drawable' attribute or child tag defining a drawable

LinearLayout使用代码

<LinearLayout
        android:id="@+id/ll_patient"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="@drawable/selector_blue47_and_white"
        android:gravity="center"
        android:orientation="vertical" >

selector的错误代码

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

selector的正确代码修改android:color为android:drawable即可

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

估计LinearLayout中是background属性,所以是需要 用drawable 而不是 color

猜你在找的XML相关文章