如何修复SeekBar滑块居中问题

41

我已经设置了SeekBar的拇指图像,但是我的拇指看起来比SeekBar下面低一点。如何将拇指设置到SeekBar的正确位置。请参考附图enter image description here

<SeekBar android:id="@+id/PP_Player_SeekBar" 
         android:thumb="@drawable/music_player_playerhead"
         android:paddingLeft="8dip"
         android:paddingRight="8dip"                 
         android:progressDrawable="@drawable/seekbar_drawable_xml_background"
         android:layout_width="236dip"
         android:layout_centerHorizontal="true"
         android:layout_height="wrap_content"
         android:layout_marginTop="47dip"></SeekBar>

感谢Sunil Kumar Saoo


我认为问题出在图片本身的“music_player_playerhead”上。所以请检查一下缩略图文件,看看圆圈是否在中间。 - Bobur Rajabov
4个回答

67

将minHeight和maxHeight设置为与seekbar的高度相同。例如:

<SeekBar 
    android:id="@+id/PP_Player_SeekBar"  
    android:thumb="@drawable/music_player_playerhead"
    android:paddingLeft="8dip"   
    android:paddingRight="8dip"                 
    android:progressDrawable="@drawable/seekbar_drawable_xml_background"
    android:layout_width="236dip" 
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content" 
    android:layout_marginTop="47dip"
    android:minHeight="11dip"
    android:maxHeight="11dip" />

请查看以下网址:http://qtcstation.com/2011/05/android-how-to-fix-seekbar-bar-thumb-centering-issues/


如果我使用了权重布局,那么高度将为0dp :) - Narendra Singh
为什么这应该有效的解释是什么?这对我没有任何影响。我的对齐在API 21及以上实际上很好,只有在旧的API上才有这个问题。我猜拇指位置正确,但背景不正确。 - Jensie

23

实际上,将maxHeight定义为一个很大的数字,例如1000dp即可。

这样做还有一个好处,就是可以与height=wrap_contentheight=match_parent一起使用。


这就是我最终的做法。在XML布局中定义一个基本的SeekBar,最大高度为1000dp。然后我可以以编程方式设置一切,拇指出现在正中央,其他所有内容也都很好。谢谢! - Joris Weimar
谢谢!这正是我需要的! - Manza
2
快速而直接。这应该是被选中的答案! 这也有一个好处,可以与 height=wrap_content 和 height=match_parent 一起使用。 - Andrew Gallasch
解决了我的问题,我将maxHeight设置为1000dp。 - Hatem Badawi

5
对我来说,问题更多地与背景(轨道可绘制对象)的垂直居中有关。这是我最初使用的有缺陷的可绘制代码(导致了问题),它是由Android开发人员和其他StackOverflow条目建议的:
<item
    android:id="@android:id/background"
    android:drawable="@drawable/seekbar_track"/>
<item android:id="@android:id/secondaryProgress">
    <scale
        android:drawable="@drawable/seekbar_progress2"
        android:scaleWidth="100%" />
</item>
<item android:id="@android:id/progress">
    <scale
        android:drawable="@drawable/seekbar_progress"
        android:scaleWidth="100%" />
</item>

这里的问题在于SeekBar的背景。许多条目会告诉你将layout_minHeight设置为某个较大的值,以避免拇指和其轨道之间出现垂直空间断开的情况。但这对我来说并不是解决方案 - 在平板电脑上查看时,背景仍然绘制为其较小的基于手机的大小 - 因此轨道始终位于SeekBar轨道的中心以上。解决方案是在SeekBar drawable中删除此条目,使其看起来像这样:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/secondaryProgress">
        <scale
            android:drawable="@drawable/seekbar_progress2"
            android:scaleWidth="100%" />
    </item>
    <item android:id="@android:id/progress">
        <scale
            android:drawable="@drawable/seekbar_progress"
            android:scaleWidth="100%" />
    </item>

</layer-list>

然后,在SeekBar的样式定义中,将layout_background设置为track drawable。我的代码如下:

<style name="styleSeekBar" parent="@android:style/Widget.SeekBar">
    <item name="android:padding">@dimen/base_0dp</item>
    <item name="android:background">@drawable/seekbar_track</item>
    <item name="android:progressDrawable">@drawable/abratingbar</item>
    <item name="android:minHeight">@dimen/base_29dp</item>
    <item name="android:maxHeight">@dimen/base_29dp</item>
    <item name="android:layout_marginLeft">@dimen/base_10dp</item>
    <item name="android:layout_marginRight">@dimen/base_10dp</item>
    <item name="android:layout_marginTop">@dimen/base_10dp</item>
    <item name="android:layout_marginBottom">@dimen/base_10dp</item>
    <item name="android:scaleType">fitXY</item>
</style>

之前,这里的背景设置只是一个颜色[白色]。

这是我布局中使用了样式和可绘制资源的入口:

<SeekBar
    android:id="@+id/seekbar_page_number"
    style="@style/styleSeekBar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/base_29dp"
    android:minHeight="@dimen/base_29dp"
    android:maxHeight="@dimen/base_29dp"
    android:layout_marginLeft="@dimen/base_230dp"
    android:layout_gravity="bottom|right"
    android:progress="1"
    android:max="20"
    android:progressDrawable="@drawable/abseekbar"
    android:thumb="@drawable/abseekbar_thumb"/>

因此,总结一下,在自定义SeekBar drawable中不要设置背景(track)功能,而是在自定义SeekBar样式的layout_background功能中设置它。这可以确保轨道始终在水平SeekBar中垂直居中。

好的答案!首先要理解是拇指还是进度条没有居中。这个解决方案适用于当进度条没有居中在你的拖动条内时。 - Bugdr0id

2
我通过将seekbar的layout_height设置为wrap_content而解决了水平滑块不对齐的问题,最初的回答。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接