Switch-Button thumb gets skewed?

后端 未结 4 1238
说谎
说谎 2020-12-31 12:28

The thumb for my Switch-button seems to get skewed(for on&off state). There were similar problems on github, but those were for people making libraries to support Switch

4条回答
  •  星月不相逢
    2020-12-31 12:56

    I had the same requirement. I checked Android code and found that

    1. switch ignores any vertical margin/padding applied to a thumb shape drawable (thus thumb always touch the top and bottom of the track)
    2. the width of the thumb is calculated by taking the horizontal paddings + the max width of the on and off texts.

    This makes really hard to make a circle thumb.

    But if you specify the thumb drawable as a layer drawable for the sole reason to be able to specify padding for the single layer of the drawable, you can get the desired effect.

    Thumb selector:

    
    
        
            
            
                
                    
                    
                        
                        
                        
                    
                
            
        
    
    

    I set the on and off text of switch to empty (actually to "" to prevent warning about empty resource).

    track:

    
    
        
            
                
                
                
            
        
        
            
                
                
                
            
        
    
    

    Switch style:

    
    

    And finally, the dimens:

    30dp
    15dp
    2dp
    

    So the only 'tricky' thing is to keep height = radius * 2.

提交回复
热议问题