【问题标题】:Custom SeekBar thumb showing unwanted background自定义 SeekBar 拇指显示不需要的背景
【发布时间】:2015-10-09 12:11:59
【问题描述】:

我一直在尝试自定义SeekBar。它应该有圆角。就连SeekBar的进度,两边也应该有圆角。我不需要拇指。像这样的东西。

为此,我制作了一个layer-list xml 文件,名为 custom_seekbar.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#F0E9DC" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="rectangle">
                <corners android:radius="20dp" />
                <solid android:color="#F0E9DC" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <corners android:radius="20dp" />
                <solid android:color="#E38F71" />
            </shape>
        </clip>
    </item>

</layer-list>  

为了处理移动的圆角,我想到了使用高度等于SeekBar 的圆形拇指。这是 thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size android:height="16dp"
        android:width="16dp"/>

    <corners android:radius="20dp"/>

    <solid android:color="#E38F71"/>

</shape>  

我在我的活动中像这样显示SeekBar

<SeekBar
        android:layout_width="250dp"
        android:layout_height="16dp"
        android:layout_centerInParent="true"
        android:progressDrawable="@drawable/custom_seekbar"
        android:thumb="@drawable/thumb" />  

唯一的问题是拇指。它没有按预期显示。它有一个不需要的背景,像这样:

如果我设置一个透明的拇指,进度不再是圆的。我什至尝试使用 椭圆形 形状的拇指。有人请告诉我我要去哪里错了吗?或者,如果有其他方法可以达到预期的效果。任何帮助将不胜感激。

【问题讨论】:

  • 创建一个自定义的Drawable 类并通过调用setProgressDrawable 来使用它,就像在this 回答中一样
  • 使用 layerlist 创建 thumb.xml
  • @user3676184 你能告诉我如何去做吗?我尝试了很多东西,但无法得到我想要的。
  • "I tried a lot many things but couldn't get what I want" 你试过自定义Drawable 吗?
  • @pskink 很抱歉没有回复您的评论。我错误地跳过了它。我没有尝试自定义可绘制类。以为我可能会错过一些非常小的东西,并且可能不需要一个全新的课程。我想这将是我最后的手段。

标签: android android-seekbar


【解决方案1】:

添加android:splitTrack="false",将解决您的问题。我也会使用android:shape="oval",而不是带有圆角的rectangle,作为您的thumb.xml

【讨论】:

  • 我使用的是“椭圆”形状。它有同样的问题。这就是为什么我尝试使用圆角矩形。仍然没有任何改变。所以我就这样发布了。
  • 您可以尝试将android:splitTrack="false" 添加到您的搜索栏吗?
  • 太棒了。只需一行,我的问题就消失了!太感谢了。你救了我几个小时!请编辑您的答案,以便我接受。
  • 这很好用!不过,您是否找到任何适用于 API 21 之前的设备的解决方案?
  • 有效!! @andreimarinescu,我在 API 21 之前的设备上尝试了我的应用程序,没有问题。上述情况仅发生在 API 21 后的设备上。
猜你喜欢
  • 1970-01-01
  • 2020-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多