Button Background Selector

前端 未结 2 1881
广开言路
广开言路 2021-01-18 02:40

I try to switch the background of Buttons if they are pressed. I build a Selector like the answer suggested here: Standard Android Button with a different color

Fin

相关标签:
2条回答
  • 2021-01-18 03:30

    put this at the end

    item android:drawable="@color/red"

    i mean as the third option, it will work. android checks the xml conditions from the start, the first tag doesn't have any condition, so it will always pick red, so you have put conditions first and then the default one.

    0 讨论(0)
  • 2021-01-18 03:38

    here is the code I use, and it works really well.

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

    here I use two images I made using photoshop as a background

    the first is button_clicked and the second is button

    copy it and change use your own resources.

    hope I could help :)

    0 讨论(0)
提交回复
热议问题