2011-08-21 78 views
9

我想创建一个自定义样式的seekbar。 我有两个九贴片图像,一个是灰色拉伸条search_progress.9.png(背景颜色),另一个是绿色拉伸条search_progress_bar.9.png(前景色)。Android seekbar设置自定义样式使用九个补丁图像

我用这个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图像)。我如何使用自己的图像获得与Android的progress_horizo​​ntal.xml相同的效果(我不想使用形状绘制我的酒吧)?

回答

11

我使用ClipDrawable解决了这个问题。 这是对我有用的东西:

Set @ drawable/search_progress_drawable as 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> 
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> 
+0

尼斯让这一切在一个文件,谢谢! – Russ

+1

工程就像一个魅力! :) – yahya

+0

它也适用于我,而我使用九个补丁图像。谢谢! – Hasmukh