如何修复seekbar-bar-thumb-centering-issues

问题描述 投票:40回答:4

我设置了一个搜索栏的拇指图像,但我的拇指看起来在搜索栏的下方。如何将拇指设置在搜索栏的正确位置。看看附图

<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

android seekbar
4个回答
64
投票

将minHeight和maxHeight设置为与搜索条的高度相同。例如

<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/


22
投票

实际上,将maxHeight定义为像1000dp这样的大数字就足够了。

这也有使用height=wrap_contentheight=match_parent的好处。


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设置为轨道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>

(以前,这里的背景设置只是一种颜色[白色]。)。

这是我的布局中的条目,它使用样式和drawable:

<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中设置背景(轨道)功能,将其设置在自定义SeekBar样式的layout_background功能中。这可确保轨道始终在水平SeekBar中垂直居中。


0
投票

我只是通过将搜索栏的layout_height设置为wrap_content而不是match_parent来解决错位的水平搜索栏拇指

© www.soinside.com 2019 - 2024. All rights reserved.