Android highlight an imagebutton when clicked

后端 未结 5 877
执笔经年
执笔经年 2021-01-31 05:06

I am using an ImageButton. But I don get the highlight when clicked. I googled and many suggested to use selector where another image is displayed. Is there any way

5条回答
  •  北海茫月
    2021-01-31 05:34

    This is actually not very difficult to do. You don't even need to create 2 seperate .png files or anything like that. For instance, if you want to have a button which has a gradient, and then change it when the button is pressed:

    Step 1: Create default button gradient (drawable/default_button.xml):

    
        
        
        
    
    

    Step 2: Create default button pressed gradient (drawable/default_button_pressed.xml):

    
        
        
        
    
    

    Step 3: Create selector (drawable/default_button_selector.xml):

    
         
         
    
    

    Step 4 (optional): Create style for the button (values/style.xml):

    
        
    
    

    Step 5: use the button (layout/main.xml):

    
    
        

    As you can see, it's not particularly difficult to do.

提交回复
热议问题