如何为离散滑块添加选定值的刻度线?

时间:2017-09-13 08:05:55

标签: android material-design android-seekbar

我试图设计一个离散的搜索栏,就像材料设计网站上显示的聚焦搜索栏一样,我无法弄清楚如何在显示所选值的栏上添加勾号

Material Design discrete sliders

我有一个有100个位置的搜索栏,这是我当前的xml代码:

<SeekBar
    android:id="@+id/seekBar"
    style="@style/Widget.AppCompat.SeekBar.Discrete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/mod_quantity_text"
    android:clickable="true"
    android:max="100"
    android:progress="1" />

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

您必须在XML中添加tickmark drable

<SeekBar
    android:id="@+id/seekBar"
    style="@style/Widget.AppCompat.SeekBar.Discrete"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/mod_quantity_text"
    android:clickable="true"
    android:tickMark="@drawable/your_tickmark_drawable"  // this line you have to add
    android:max="100"
    android:progress="1" />

答案 1 :(得分:1)

我已经有了解决方案来创建一个名为&#34; thumb.xml&#34;包含对png文件的引用和参数android:bottom设置为适当的级别,以保证拇指在搜索栏上的完美对齐(在我的情况下为50dp):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
    android:bottom="50dp"
    android:drawable="@drawable/seekbar_thumb"/>
</layer-list>

在此之后我更改了布局xml文件中的SeekBar参数,添加了一个自定义拇指(和相关颜色):

<SeekBar
        android:id="@+id/seekBar"
        style="@style/Widget.AppCompat.SeekBar"
        android:layout_width="match_parent"
        android:layout_height="95dp"
        android:minHeight="20dp"
        android:clickable="true"
        android:progressBackgroundTint="@color/colorPrimary"
        android:max="100"
        android:thumb="@drawable/thumb"
        android:thumbTint="@color/colorAccent"
        android:progress="1"/>

添加tickmark drawable并不适用于我,因为我需要改变并移动拇指。 希望这会有所帮助