Can I increase a buttons onclick-area programmatically?

后端 未结 8 1981
孤独总比滥情好
孤独总比滥情好 2021-02-01 16:34

Sometimes I have a button in my UI that it is so small that it is difficult to click. My solution so far has been to add a transparent border around the button in photoshop. Jus

8条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 17:11

    This is a very late "me too," but after coming to this and other questions looking for a solution, I found a simple, elegant solution of my own.

    Another question complained that the transparent background of their image was not clickable. If that is an issue, this seems to get around that as well.

    Here's the button:

    
    

    The relevant lines are the last two. "@drawable/arrow_up" is a few button states in *.png files defined in a drawable *.xml file like this:

    
    
      
      
      
      
    
    

    Just your basic button. Nothing special. And "@drawable/clear_button_background" is just this:

    
    
       
       
    
    

    The height and width here are the clickable area, resize as needed. You can reuse this for as many buttons as you need in a single view, unlike the absurdly detailed TouchDelegate. No additional listeners. It doesn't add any views or groups to your hierarchy and you won't be messing around with padding and margins all day.

    Simple. Elegant.

提交回复
热议问题