<item> tag requires a 'drawable' attribute"-selector设置颜色报错-原因及解决方案

“ tag requires a ‘drawable’ attribute”-selector设置颜色报错-原因及解决方案

1.关于设置Button等的状态选择器的报错

  • 首先贴上代码:

    创建:main\res\drawable\seletor_usercenter.xml
      <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- 定义按钮按下时的图片 -->
            <item android:colo="@color/color_green" android:state_pressed="true" />
            <item  android:color="@color/color_green" android:state_focused="true" />
            <item android:color="@color/color_green" android:state_pressed="true"/>
    
            <!-- 定义按钮默认的图片 -->
            <item android:color="@color/color_white"/>
        </selector>
    
    选择器设置于Button的background:
            <Button
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:background="@drawable/seletor_usercenter/>
    
  • 运行报错:

    报错信息:
        android.view.InflateException: Binary XML file line #46: Error inflating class <unknown>
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)
        at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
        at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
        ...
        Caused by: android.content.res.Resources$NotFoundException: 
        File res/drawable/seletor_usercenter.xml from drawable resource ID #0x7f020072
        at android.content.res.Resources.loadDrawable(Resources.java:2096)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
        at android.view.View.<init>(View.java:3554)
        at android.widget.TextView.<init>(TextView.java:623)
        at android.widget.Button.<init>(Button.java:107)
        at android.widget.Button.<init>(Button.java:103)
        ...
        Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #13: <item> tag requires a 
        'drawable' attribute or child tag defining a drawable
        at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:181)
        at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
        at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
        at android.content.res.Resources.loadDrawable(Resources.java:2092)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:602) 
        at android.view.View.<init>(View.java:3554)
        ...
    
    **分析:报错的信息提示item标签下需要drawable属性,这下子可头大了,我设置的是颜色选择器,报错信息却说item标签下需要
          drawable属性,我上哪找drawable属性??? 寻找解决方法 ...**
    
  • 验证猜想:

    是否将<item android:colo="@color/color_green"   android:state_pressed="true" />中的"color"改为"drawable"?
    
    果断改了之后,再贴代码:
        <?xml version="1.0" encoding="utf-8"?>
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
                <!-- 定义按钮按下时的图片 -->
                <item android:drawable="@color/color_green"android:state_selected="true" />
                <item android:drawable="@color/color_green" android:state_pressed="true" />
                <item android:drawable="@color/color_green" android:state_focused="true" />
                <!-- 定义按钮默认的图片 -->
                <item android:drawable="@color/color_white" />
                    </selector>
    
    结果:运行成功!
    
  • 疑问:为什么item标签设置color属性会报错,而drawable属性则不会报错

    探讨:
    发现按钮的背景图片不支持颜色选择器,只支持drawable选择器,而Button的"textColor"属性则支持颜色选择器
    
    可这样设置:
            <Button
                android:layout_width="match_parent"
                android:layout_height="54dp"
                android:textColor="@drawable/seletor_usercenter"/>
    
            <selector xmlns:android="http://schemas.android.com/apk/res/android">
                <!-- 定义按钮按下时的图片 -->
                <item android:color="@color/color_green" android:state_pressed="true" />
                <item  android:color="@color/color_green" android:state_focused="true" />
                <item android:color="@color/color_green" android:state_pressed="true"/>
    
                <!-- 定义按钮默认的图片 -->
                <item android:color="@color/color_white"/>
            </selector>
    
    运行之后是可行的,这就说明探讨的猜想是正确的。经过探讨,这都源于android:background="@drawable/
    seletor_usercenter" 被Android框架解析成StateListDrawable类对象。而当我们在main\res\drawable
    \seletor_usercenter.xml文件里设置color,就出现报错信息提示的"StateListDrawable.inflate"错误信息的异常
    

2.设置drawable选择器的方式

  • 第一种上面所列的:

    <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android">
            <!-- 定义按钮按下时的图片 -->
            <item android:drawable="@color/color_green"android:state_selected="true" />
            <item android:drawable="@color/color_green" android:state_pressed="true" />
            <item android:drawable="@color/color_green" android:state_focused="true" />
            <!-- 定义按钮默认的图片 -->
            <item android:drawable="@color/color_white" />
        </selector>
    
  • 第二种:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- 定义按钮按下时的图片 -->
        <item android:state_selected="true" >
            <shape>
                <solid android:color="@color/color_green"/>
            </shape>
        </item>
        <!-- 定义按钮默认的图片 -->
        <item  android:state_selected="false">
            <shape>
                <solid android:color="@color/color_white"/>
            </shape>
        </item>
        </selector>
    

    其实这种方法将selector和颜色的定义分开了 我们可以将selector和shape的定义放在一起

3.总结:控件 比如Button的背景图片不支持颜色选择器,只支持drawable选择器,而Button的”textColor”属性则支持颜色选择器
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值