如何为离散滑块显示刻度标记?

12

图片描述

我正在尝试为一个seekbar/slider添加样式,就像Material Design Guidelines中标记为“Discrete Slider - Click”的(有小刻度标记的)那个一样。但我无法找到将刻度标记显示出来的方法,是否有人知道如何做到这一点?

我有一个包含5个位置(0-4)的seekbar。

<SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="4" />
3个回答

22

使用style属性添加刻度标记:

<SeekBar
    android:id="@+id/seekBar"
    style="@style/Widget.AppCompat.SeekBar.Discrete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="10"
    />

或者通过设置tickMark drawable来手动添加它们:

<SeekBar
    android:id="@+id/seekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="10"
    android:tickMark="@drawable/tickmark"
    />

勾号标记.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="oval">
    <size android:width="4dp"
          android:height="4dp"/>
    <solid android:color="@android:color/white"/>
</shape>

5
看起来这是最近在24.x.x支持库中添加的(在23.4.0中似乎不可用)。 - jt-gilkeson
@NameSpace,我在SeekBar上没有得到滴答指示器,你能帮我吗? - Pankaj
1
@NameSpace 滑动条如何知道步长以放置刻度标记。 - Pooja Kamath
它从“max”值中知道这一点。当最大值为10时,它将把SeekBar分成10个段,并在每个段之间放置一个刻度标记。 - Christian

3
现在你可以在 Material Components 库中使用 滑块
离散滑块默认显示刻度标记。
    <com.google.android.material.slider.Slider
        android:valueFrom="0"
        android:valueTo="10"
        android:stepSize="1"
        .../>

enter image description here


0

使用 com.google.android.material.slider.Slider

app:tickVisible="true" // false

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接