How to set button click effect in Android?

后端 未结 14 2360
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-28 01:29

In Android, when I set background image to Button, I can not see any effect on button.

I need some effect on button so user can recognize that button is clicked.

14条回答
  •  借酒劲吻你
    2020-11-28 01:44

    This can be achieved by creating a drawable xml file containing a list of states for the button. So for example if you create a new xml file called "button.xml" with the following code:

    
        
        
        
        
    
    

    To keep the background image with a darkened appearance on press, create a second xml file and call it gradient.xml with the following code:

    
    
        
            
        
        
            
                
            
        
    
    

    In the xml of your button set the background to be the button xml e.g.

    android:background="@drawable/button"
    

    Hope this helps!

    Edit: Changed the above code to show an image (YOURIMAGE) in the button as opposed to a block colour.

提交回复
热议问题