Android: How to Make A Drawable Selector

孤者浪人 提交于 2019-12-28 03:01:46

问题


I feel like this is kind of a silly question, but here I go anyways. I have an image button, and I want to be able to change it's image everytime it is clicked. The API seems to say that the best way to go about doing this is to create xml resource in the drawable folder that contains a selector and values. However when I go to make a new android xml resource, there's no option for drawables. What am I missing?


回答1:


As far as I know, the Android XML editor doesn't allow you to create XML drawables. You have to go to the source tab (labeled: filename.xml) and paste in the text manually. It should look like:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" 
        android:drawable="@drawable/cell_top_selected" />
    <item android:drawable="@drawable/cell_top" />
</selector>



回答2:


You can try this also as a selector

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

    <!-- pressed -->
    <item android:drawable="@drawable/button_1_selected" android:state_pressed="true"/>
    <!-- focused -->
    <item android:drawable="@drawable/button_1_normal" android:state_focused="true"/>
    <!-- default -->
    <item android:drawable="@drawable/button_1_normal"/>

</selector>


来源:https://stackoverflow.com/questions/5624609/android-how-to-make-a-drawable-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!