Exception android.content.res.Resources$NotFoundException: File res/drawable/my.xml from drawable resource ID

前端 未结 2 754
没有蜡笔的小新
没有蜡笔的小新 2020-12-31 03:00

Whats wrong with this xml, I am trying to get a button press feel. I am getting a exception, I am unable to understood. ResourceNotFound

In my layout.xm

相关标签:
2条回答
  • 2020-12-31 03:48

    The attribute android:background does not exist. The correct way to do this is to first create a new resource file res/colors.xml with the following contents:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="lt_gray">#AAAAAA</color>
        <color name="dk_gray">#777777</color>
    </resources>
    

    Then just change your selector to the following:

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

    This automatically creates a ColorDrawable and assigns it as the drawable for that state.

    0 讨论(0)
  • 2020-12-31 03:51

    Once Try this

    In selector state it is required to add drawable and in your case you wants to add color in selector state.So it is possible to create drawable using color in resource. It will work as ColorDrawable

    Please check below code

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

    values/string.xml for color

     <drawable name="clr_normal">#AAAAAA</drawable>
    <drawable name="clr_pressed">#777777</drawable>
    
    0 讨论(0)
提交回复
热议问题