Search code examples
androidandroid-drawableandroid-seekbarlayer-list

Seekbar progressdrawable not work correctly below Android M


I have a seekbar whose height is 16dp.

minHeight="16dp"
maxHeight="16dp"

Now I have to use custom progress 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">
    <shape android:shape="rectangle">
        <solid android:color="xxx"/>
        <corners android:radius="6dp"/>
    </shape>
</item>

<item
    android:id="@android:id/secondaryProgress"
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp">
    <clip>
        <shape android:shape="rectangle">
            <solid android:color="xxx"/>
            <corners android:radius="4dp"/>
        </shape>
    </clip>
</item>

<item
    android:id="@android:id/progress"
    android:bottom="4dp"
    android:left="4dp"
    android:right="4dp"
    android:top="4dp">
    <clip>
        <shape android:shape="rectangle">
            <solid android:color="xxx"/>
            <corners
                android:radius="4dp"/>
        </shape>
    </clip>
</item>

As you see, I use left,top,right,bottom to insert progress and secondaryProgress.

It works well above Android M,however below Android M,the porgress and secondaryProgress fills the background in other word the background item is covered by progress and secondaryProgress.But I want progress and secondaryProgress smaller than background item.


Solution

  • There is a solution:

    LayerDrawable drawable = (LayerDrawable) mSeekBar.getProgressDrawable();
    // secondaryProgress
    drawable.setLayerInset(1, padding, padding, padding, padding);
    // progress
    drawable.setLayerInset(2, padding, padding, padding, padding);
    

    Can anyone explain why it's useless to set left right top bottom in xml below API 23?