Custom seekbar (thumb size, color and background)

前端 未结 10 2151
醉梦人生
醉梦人生 2020-11-28 04:07

I would like to have this seekbar in my Android project :

This is my seekbar :



        
相关标签:
10条回答
  • 2020-11-28 04:37

    For future readers!

    Starting from material-components-android 1.2.0-alpha01, you can use new slider component

    ex:

    Modify thumbSize, thumbColor, trackColor accordingly.

    <com.google.android.material.slider.Slider
            android:id="@+id/slider"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:valueFrom="20f"
            android:valueTo="70f"
            android:stepSize="10"
            app:thumbRadius="20dp"
            app:thumbColor="@color/colorAccent"
            app:trackColor="@android:color/darker_gray"
            />
    

    Note: Track corners are not round.

    0 讨论(0)
  • 2020-11-28 04:38

    You can use the official Slider in the Material Components Library.

    Use the app:trackHeight="xxdp" (default value is 4dp) to change the height of the track bar.

    Also use these attributes to customize the colors:

    • app:activeTrackColor: the active track color
    • app:inactiveTrackColor: the inactive track color
    • app:thumbColor: to fill the thumb

    Something like:

        <com.google.android.material.slider.Slider
            android:id="@+id/slider"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:activeTrackColor="#ffd400"
            app:inactiveTrackColor="#e7e7e7"
            app:thumbColor="#ffb300"
            app:trackHeight="12dp"
            .../>
    

    It requires the version 1.2.0 of the library.

    0 讨论(0)
  • 2020-11-28 04:38

    android:minHeight android:maxHeight is important for that.

    <SeekBar
        android:id="@+id/pb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:maxHeight="2dp"
        android:minHeight="2dp"
        android:progressDrawable="@drawable/seekbar_bg"
        android:thumb="@drawable/seekbar_thumb"
        android:max="100"
        android:progress="50"/>
    

    seekbar_bg.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>
                <corners android:radius="3dp" />
                <solid android:color="#ECF0F1" />
            </shape>
        </item>
        <item android:id="@android:id/secondaryProgress">
            <clip>
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#C6CACE" />
                </shape>
            </clip>
        </item>
        <item android:id="@android:id/progress">
            <clip>
                <shape>
                    <corners android:radius="3dp" />
                    <solid android:color="#16BC5C" />
                </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="oval">
        <solid android:color="#16BC5C" />
        <stroke
            android:width="1dp"
            android:color="#16BC5C" />
        <size
            android:height="20dp"
            android:width="20dp" />
    </shape>
    
    0 讨论(0)
  • 2020-11-28 04:43

    Android custom SeekBar - custom track or progress, shape, size, background and thumb and for other seekbar customization see http://www.zoftino.com/android-seekbar-and-custom-seekbar-examples

    Custom Track drawable

    <?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:gravity="center_vertical|fill_horizontal">
            <shape android:shape="rectangle"
                android:tint="#ffd600">
                <corners android:radius="8dp"/>
                <size android:height="30dp" />
                <solid android:color="#ffd600" />
            </shape>
        </item>
        <item android:id="@android:id/progress"
            android:gravity="center_vertical|fill_horizontal">
            <scale android:scaleWidth="100%">
                <selector>
                    <item android:state_enabled="false"
                        android:drawable="@android:color/transparent" />
                    <item>
                        <shape android:shape="rectangle"
                            android:tint="#f50057">
                            <corners android:radius="8dp"/>
                            <size android:height="30dp" />
                            <solid android:color="#f50057" />
                        </shape>
                    </item>
                </selector>
            </scale>
        </item>
    </layer-list>
    

    Custom thumb drawable

    ?xml version="1.0" encoding="utf-8"?>
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle"
        android:thickness="4dp"
        android:useLevel="false"
        android:tint="#ad1457">
        <solid
            android:color="#ad1457" />
        <size
            android:width="32dp"
            android:height="32dp" />
    </shape>
    

    Output

    0 讨论(0)
提交回复
热议问题