使用九宫格图片设置自定义样式的Android SeekBar

9
我将尝试创建一个自定义样式的进度条。 我有两个九宫格图片,一个是灰色拉伸条search_progress.9.png(背景颜色),另一个是绿色拉伸条search_progress_bar.9.png(前景颜色)。
我使用这个XML作为我的进度条的progressDrawable:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background" android:drawable="@drawable/search_progress"></item>
    <item android:id="@android:id/progress" android:drawable="@drawable/search_progress_bar"></item>
</layer-list>

我的问题是,进度条不是到达拇指位置就变成绿色,而是一直保持绿色(search_progress_bar image)。我该如何使用自己的图片来实现与Android的progress_horizontal.xml相同的效果(我不想使用形状来绘制进度条)?

2个回答

16

e_x_p的版本可以很好地工作,但更简单的版本是

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

    <item android:id="@android:id/background">
        <nine-patch android:src="@drawable/progressbar_bg" android:dither="true"/>
    </item>
    <item android:id="@android:id/progress">
        <clip xmlns:android="http://schemas.android.com/apk/res/android"
            android:clipOrientation="horizontal"
            android:gravity="left">
            <nine-patch android:src="@drawable/progressbar_status" android:dither="true"/>
        </clip>
    </item>

</layer-list>

当我使用Nine Patch图像时,这对我也起作用。谢谢! - Hasmukh

11
我使用ClipDrawable解决了这个问题。 以下是我用的方法:
将@drawable/search_progress_drawable设置为progressDrawable。
search_progress_drawable.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" android:drawable="@drawable/search_progress"></item>
    <item android:id="@android:id/progress" android:drawable="@drawable/search_progress_clip"></item>
</layer-list>

search_progress_clip.xml:

<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/search_progress_bar"
    android:clipOrientation="horizontal"
    android:gravity="left">
</clip>

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