Custom seekbar (thumb size, color and background)

前端 未结 10 2147
醉梦人生
醉梦人生 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:22

    At first courtesy goes to @Charuka .

    DO

    You can use android:progressDrawable="@drawable/seekbar" instead of android:background="@drawable/seekbar" .

    progressDrawable used for the progress mode.

    You should try with

    android:minHeight

    Defines the minimum height of the view. It is not guaranteed the view will be able to achieve this minimum height (for example, if its parent layout constrains it with less available height).

    android:minWidth

    Defines the minimum width of the view. It is not guaranteed the view will be able to achieve this minimum width (for example, if its parent layout constrains it with less available width)

        android:minHeight="25p"
        android:maxHeight="25dp" 
    

    FYI:

    Using android:minHeight and android:maxHeight is not good solutions .Need to rectify your Custom Seekbar (From Class Level) .

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

    Use tints ;)

    <SeekBar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="15dp"
            android:minWidth="15dp"
            android:maxHeight="15dp"
            android:maxWidth="15dp"
            android:progress="20"
            android:thumbTint="@color/colorPrimaryDark"
            android:progressTint="@color/colorPrimary"/>
    

    use the color you need in thumbTint and progressTint. It is much faster! :)

    Edit ofc you can use in combination with android:progressDrawable="@drawable/seekbar"

    0 讨论(0)
  • 2020-11-28 04:24
    • First at all, use android:splitTrack="false" for the transparency problem of your thumb.

    • For the seekbar.png, you have to use a 9 patch. It would be good for the rounded border and the shadow of your image.

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

    All done in XML (no .png images). The clever bit is border_shadow.xml.

    All about the vectors these days...

    Screenshot:


    This is your SeekBar (res/layout/???.xml):

    SeekBar

    <SeekBar
        android:id="@+id/seekBar_luminosite"
        android:layout_width="300dp"
        android:layout_height="wrap_content"        
        android:progress="@integer/luminosite_defaut"
        android:progressDrawable="@drawable/seekbar_style"
        android:thumb="@drawable/custom_thumb"/>
    

    Let's make it stylish (so you can easily customize it later):

    style

    res/drawable/seekbar_style.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/border_shadow" >
        </item>
    
        <item 
            android:id="@android:id/progress" > 
            <clip 
                android:drawable="@drawable/seekbar_progress" />
        </item>
    </layer-list>
    

    thumb

    res/drawable/custom_thumb.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item>
            <shape android:shape="oval">
                <solid android:color="@color/colorDekraOrange"/>
                <size
                    android:width="35dp"
                    android:height="35dp"/>
            </shape>
        </item>   
    </layer-list>
    

    progress

    res/drawable/seekbar_progress.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list 
        xmlns:android="http://schemas.android.com/apk/res/android" >
        <item 
            android:id="@+id/progressshape" >
            <clip>
                <shape 
                    android:shape="rectangle" >
                    <size android:height="5dp"/>
                    <corners 
                        android:radius="5dp" />
                    <solid android:color="@color/colorDekraYellow"/>        
                </shape>
            </clip>
        </item>
    </layer-list>
    

    shadow

    res/drawable/border_shadow.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">       
        <item>      
            <shape> 
                <corners 
                    android:radius="5dp" />
                <gradient
                    android:angle="270"
                    android:startColor="#33000000"
                    android:centerColor="#11000000"
                    android:endColor="#11000000"
                    android:centerY="0.2"
                    android:type="linear"
                />          
            </shape> 
        </item>
    </layer-list>
    
    0 讨论(0)
  • 2020-11-28 04:30

    You can try progress bar instead of seek bar

    <ProgressBar
        android:id="@+id/progressBar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:layout_marginBottom="35dp"
        />
    
    0 讨论(0)
  • 2020-11-28 04:35

    No shadow and no rounded borders in the bar

    You are using an image so the easiest solution is row your boat with the flow,

    You cannot give heights manually,yes you can but make sure it gets enough space to show your full image view there

    • easiest way is use android:layout_height="wrap_content" for SeekBar
    • To get more clear rounded borders you can easily use the same image that you have used with another color.

    I am no good with Photoshop but I managed to edit a background one for a test

    seekbar_brown_to_show_progress.png

    <SeekBar
        android:splitTrack="false"   // for unwanted white space in thumb
        android:id="@+id/seekBar_luminosite"
        android:layout_width="250dp"   // use your own size
        android:layout_height="wrap_content"
        android:minHeight="10dp"
        android:minWidth="15dp"
        android:maxHeight="15dp"
        android:maxWidth="15dp"
        android:progress="50"
        android:progressDrawable="@drawable/custom_seekbar_progress"
        android:thumb="@drawable/custom_thumb" />
    

    custom_seekbar_progress.xml

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <item
            android:id="@android:id/background"
            android:drawable="@drawable/seekbar" />
        <item android:id="@android:id/progress">
            <clip android:drawable="@drawable/seekbar_brown_to_show_progress" />
        </item>
    </layer-list>
    

    custom_thumb.xml is same as yours

    Finally android:splitTrack="false" will remove the unwanted white space in your thumb

    Let's have a look at the output :

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