6

我想对我的搜索栏的最小值和最大值都有一个拇指。您应该能够独立拖动两个拇指。

4

2 回答 2

12

只需使用RangeSliderin Material Components 和方法setValues()

    <com.google.android.material.slider.RangeSlider
        android:id="@+id/slider"            
        android:valueFrom="0"
        android:valueTo="10"
        ../>

和:

RangeSlider slider = findViewById(R.id.slider);
slider.setValues(1.0f,5.0f);

在此处输入图像描述

您还可以使用:

    <com.google.android.material.slider.RangeSlider
        android:id="@+id/slider"            
        android:valueFrom="0"
        android:valueTo="10"
        app:values="@array/initial_slider_values"
        ../>

res/values/arrays.xml

<resources>
  <array name="initial_slider_values">
    <item>1.0</item>
    <item>5.0</item>
  </array>
</resources>

注意:这需要最低版本1.2.0-beta01

于 2020-04-08T08:31:08.823 回答
3

android中的普通搜索栏不能有两个可以更改的拇指。

而是使用 Material Compoments Library 中的 Slider。 https://material.io/components/sliders/#

我们需要包含的依赖项是:

implementation 'com.google.android.material:material:1.2.0-alpha05'

测试此滑块的示例布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

  <com.google.android.material.slider.Slider
      android:id="@+id/slider"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:value="12.34"
      android:valueFrom="0.0"
      android:valueTo="50.0" />

  <com.google.android.material.slider.Slider
    android:id="@+id/rangeSlider"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:valueFrom="0.0"
    android:valueTo="100.0" />
</LinearLayout>

在您的活动中,使用两个拇指设置范围滑块:

Slider rangeSlider = findViewById(R.id.rangeSlider);
rangeSlider.setValues(0.0F, rangeSlider.getMaximumValue());

在您的 styles.xml 中,从 AppCompat 更改为 MaterialComponents,如下所示:

From: parent="Theme.AppCompat.Light.DarkActionBar">
To:   parent="Theme.MaterialComponents.Light.DarkActionBar">

现在我们有了一个带有两个拇指的滑块! 范围滑块

于 2020-03-15T12:20:51.373 回答