简体   繁体   中英

Android SeekBar thumb gets clipped/cut off

When using a custom thumb drawable with a SeekBar view, the thumb drawable is clipped at the left and right edges of the view.

How can I fix this behavior?

You should be able to fix this by setting paddingLeft and paddingRight on your SeekBar to half the thumb width (remember to use density-independent units). You can also control the space allowed at the edges for a seek bar's thumb by calling setThumbOffset .

我自己遇到了这个问题,我相信"correct"决定是修改android:thumbOffset作为SeekBar的默认样式将其设置为 8px。

For default SeekBar I used these settings and it works fine:

android:paddingLeft="6dp"
android:paddingRight="6dp"

android:thumbOffset="8dp"

Just for clarification.

On some places I have seen

 android:thumbOffset="8dp"

and some

android:thumbOffset="8px"

so I looked at the source code. this is the original style

<style name="Widget.SeekBar">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:indeterminateDrawable">@android:drawable/progress_horizontal</item>
        <item name="android:minHeight">20dip</item>
        <item name="android:maxHeight">20dip</item>
        <item name="android:thumb">@android:drawable/seek_thumb</item>
        <item name="android:thumbOffset">8dip</item>
        <item name="android:focusable">true</item>
    </style>

from

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/styles.xml

I was getting the bottom of my default seekbar thumb clipped. None of the fixes here worked. I ended up using

     android:clipToPadding="false"

on the parent view. This solved issue.

<androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/progress_loan_period"
        android:layout_width="@dimen/dp_0"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/dp_15"
        android:progressDrawable="@drawable/seek_bar"
        android:thumb="@drawable/ic_slider"
        android:paddingStart="@dimen/dp_0"
        android:paddingEnd="@dimen/dp_0"
        android:thumbOffset="-0dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/label_loan_period" />

This gets the job done

没有什么对我有用,但我只是改变了拇指的颜色,而不是父布局的背景颜色,它对我有用。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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