【问题标题】:how to fix seekbar-bar-thumb-centering-issues如何修复 seekbar-bar-thumb-centering-issues
【发布时间】:2011-09-13 15:03:35
【问题描述】:

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

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

谢谢 苏尼尔·库马尔·萨乌

【问题讨论】:

  • 我认为问题在于图像本身“music_player_playerhead”。如果中间有圆圈,请检查拇指文件。

标签: android seekbar


【解决方案1】:

将 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 :)
  • 什么解释了为什么这应该起作用?这对我没有任何作用。我的对齐实际上在 API 21 和更高版本上都很好,我只在较旧的 API 上遇到这个问题。我的猜测是拇指正确对齐,但不是背景。
【解决方案2】:

其实把maxHeight定义成1000dp这样的大数就够了。

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

【讨论】:

  • 这就是我最终要做的。在 XML 布局中定义一个基本的 SeekBar,maxHeight 为 1000dp。然后我可以以编程方式设置所有内容,拇指出现在死点,其他一切都很好。谢谢!
  • 谢谢!这就是我需要的!
  • 快速且切中要害。这应该是选择的答案!这也有使用 height=wrap_content 和 height=match_parent 的好处
  • 为我解决问题的我将 maxHeight 设置为 1000dp
【解决方案3】:

对我来说,这个问题更多地与背景的垂直居中有关(可绘制轨道)。这是我最初使用的有缺陷的可绘制代码(产生了问题),正如 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 可绘制对象中设置背景(轨道)功能,而是在自定义 SeekBar 样式的 layout_background 功能中设置它。这可确保轨道始终在水平 SeekBar 中垂直居中。

【讨论】:

  • 好答案!首先尝试了解它的拇指或进度是否未居中。此解决方案适用于进度不在搜索栏中居中时
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多