how to fix seekbar-bar-thumb-centering-issues

后端 未结 4 1364
予麋鹿
予麋鹿 2020-12-08 09:43

I have set thumb image of a seek bar but my thumb looks little below of seek bar. How to set thumb at proper position of a seekbar. Have a look on the attached image

4条回答
  •  情话喂你
    2020-12-08 09:58

    For me, the issue is more related to the vertical centering of the background (track drawable). This is the flawed drawable code I used originally (which generated the problem), as suggested by Android developer and other StackOverflow entries:

    
    
        
    
    
        
    
    

    The problem here is the first item, which relates to the background of the SeekBar. Many entries will tell you to set the layout_minHeight feature to some large value to avoid a vertical spatial disconnect between the thumb and its track. This was not the solution for me - when viewed on a tablet, the background was still drawing to its smaller phone-based size - so the track was consistently positioned well above the center of the SeekBar track. The solution is to remove this entry in the SeekBar drawable, so it now looks like this:

    
    
        
            
        
        
            
        
    
    
    

    Then, in the style definition of the SeekBar, set the layout_background to the the track drawable. Mine looks like this:

    
    

    (Previously, the background setting here was just a color [white].).

    This is the entry in my layout, which uses both the style and the drawables:

    
    

    So, to summarize, do not set the background (track) feature in your custom SeekBar drawable, set it in the layout_background feature of your custom SeekBar style. This ensures the track is always vertically centered in a horizontal SeekBar.

提交回复
热议问题