如何在SeekBar中隐藏/显示拇指绘图

时间:2013-12-31 10:26:27

标签: android user-interface

我有一个带有自定义drawable的SeekBar用于Thumb,我希望能够根据我的另一个控件显示/隐藏它。

我尝试从资源加载drawable,然后将SeekBar.setThumb()与drawable一起使用,或者为null。

隐藏它(设置为null),但我永远无法收回它。

4 个答案:

答案 0 :(得分:22)

最好的方法是从XML设置拇指的drawable(就像我一直在做的那样),然后当你想隐藏/显示Thumb drawable时,只需操纵它的alpha值:

// Hide the thumb drawable if the SeekBar is disabled
if (enabled) {
    seekBar.getThumb().mutate().setAlpha(255);
} else {
    seekBar.getThumb().mutate().setAlpha(0);
}

答案 1 :(得分:5)

通过xml

在SeekBar中隐藏你的拇指
 android:thumbTint="@color/transparent"

例如

<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:thumb="@color/gray"
android:thumbTint="@android:color/transparent" />

答案 2 :(得分:1)

您可以通过设置任意大的拇指偏移值来隐藏搜索栏拇指,这会将拇指移出视野。例如,

mySeekBar.setThumbOffset(10000);    // moves the thumb out of view (to left)

要再次显示拇指,请将偏移设置为零或其他常规值:

mySeekBar.setThumbOffset(0);        // moves the thumb back to view

此方法适用于所有API级别。

答案 3 :(得分:1)

最简单的方法是

<SeekBar
        android:id="@+id/seekbar_id"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:thumb="@android:color/transparent" />

就这样,一行

android:thumb="@android:color/transparent"