【问题标题】:Custom thumb for a seekbar in AndroidAndroid中搜索栏的自定义拇指
【发布时间】:2015-09-12 08:08:46
【问题描述】:

我想为搜索栏创建一个自定义拇指,应该如下所示:

一种解决方案可能是this one,其中使用 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)的所有图片。

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

【问题讨论】:

    标签: android seekbar custom-selectors


    【解决方案1】:

    我无法仅使用 xml 找到解决方案,这就是我使用图片解决此问题的原因,在 this answer 的帮助下:

    1. 创建了拇指 seekbar_thumb_icon.png 的图像,并将其以不同的分辨率保存在 res/drawable-* 地图 (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>
    

    【讨论】:

    • 这行得通吗? android:thumb="@layout/seekbar_thumb" ??
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    相关资源
    最近更新 更多