Android highlight an imagebutton when clicked

后端 未结 5 879
执笔经年
执笔经年 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:40

    I managed to do this!! First you declare buttonStyle.xml in drawable folder.

        
    
      
    
      
    
      
    
    

    Then you create button_pressed.xml in drawable folder.

    
    
      
      
    
    

    After that, you create button_normal.xml in drawable folder.

    
    
      
      
    
    

    You can use default colors but if you want to do it like me, you need to have a file named colors.xml in values folder and this values inside:

    
    
    #2A3748
      #602A3748
    
    @color/gnfrBlueColor
        @color/semiTransparentGnfrBlueColor
      
    
    

    Finally you set this to your button or whatever:

    savedAccountLoginButton.SetBackgroundResource(Resource.Drawable.buttonStyle);
    

    NOTICE THAT I set a stroke with color transparent because when you set backgroundresource your button becomes bigger and with this trick it remains original.

    This is the only way I could archieve this programmatically.

    If you want to do this by XML:

提交回复
热议问题