【发布时间】: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 很抱歉没有回复您的评论。我错误地跳过了它。我没有尝试自定义可绘制类。以为我可能会错过一些非常小的东西,并且可能不需要一个全新的课程。我想这将是我最后的手段。