开发者

Android: How to Make A Drawable Selector

开发者 https://www.devze.com 2023-02-23 10:05 出处:网络
I feel like t开发者_C百科his 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 th

I feel like t开发者_C百科his 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?


You can add this in Android Studio, use Right click on project structure -> New -> Drawable resource file. It should look like this:

<?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>


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消