繁体   English   中英

Android中搜索栏的自定义拇指

[英]Custom thumb for a seekbar in Android

我想为seekbar创建一个自定义拇指,如下所示:

在此输入图像描述

一种解决方案可以是这一种 ,其中png图片用于绘制拇指。

我认为应该可以只使用xml,因为它与这个拇指非常相似:

thumb.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">

    <size android:height="30dp" android:width="30dp"/>
    <stroke android:width="18dp" android:color="#882EA5DE"/>
    <solid android:color="#2EA5DE" />
    <corners android:radius="1dp" />

</shape>

在此输入图像描述

只需要添加第二个边框(周围的白色笔划),这样我就可以跳过来管理不同屏幕分辨率的所有图片(hdpi / mdpi / xhdpi / xxhdpi)。

我尝试了不同的组合形状“椭圆形”和“环形”,但无法得到所需的结果。 你怎么能做到的?

我无法找到只有xml的解决方案,这就是为什么我使用图片来解决这个问题,在这个答案的帮助下:

  1. 创建了thumb seekbar_thumb_icon.png的图像并将其保存在res/drawable-* maps(hdpi / mdpi / xhdpi / xxhdpi)中的不同分辨率中。

  2. 为SeekBar指定“android:thumb =”@ layout / seekbar_thumb“:

 <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <item> <shape> <size android:height="30dp" android:width="30dp" /> <solid android:color="@android:color/transparent" /> </shape> </item> <item android:drawable="@drawable/balance_icon_48px"/> </layer-list> 

在此输入图像描述

其他款式:

<SeekBar
android:id="@+id/seekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progressDrawable="@layout/seekbar_style"
android:thumb="@layout/seekbar_thumb" />

seekbar_style.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="match_parent"
    android:layout_width="match_parent">

    <item android:id="@android:id/background"
        android:drawable="@layout/seekbar_background" />

    <item android:id="@android:id/progress">
        <clip android:drawable="@layout/seekbar_progress" />
    </item>
</layer-list>

seekbar_background.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:shape="line">

   <stroke android:width="2dp" android:color="#868686"/>
   <corners android:radius="1dp" />
</shape>

seekbar_progress.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:shape="line">

   <stroke android:width="2dp" android:color="#ffffff"/>
   <corners android:radius="1dp" />
</shape>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM