我正在尝试实现一个具有固定值的离散滑块,但我唯一可以设置的是valueFrom、valueTo和stepSize。
这是我想要做的代码
<com.google.android.material.slider.Slider
android:id="@+id/slider"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="8dp"
app:tickColor="@color/colorSecondaryLight"
app:tickColorActive="@color/colorSecondary" />
有办法在滑块上设置固定值吗?(使用2、5、10、25、50和100的值)
发布于 2021-07-15 23:14:32
对我有用的解决方案是这个名为BubbleSeekBar的库。
步骤1-添加您的等级上的依赖项。
implementation "com.xw.repo:bubbleseekbar:3.20-lite"
步骤2-在XML上创建BubbleSeekBar并添加所需的属性。对于我的案例,下面的例子起了作用。
<com.xw.repo.BubbleSeekBar
android:id="@+id/bubbleSeekBar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:bsb_auto_adjust_section_mark="true"
app:bsb_hide_bubble="true"
app:bsb_min="2"
app:bsb_section_count="5"
app:bsb_section_text_position="below_section_mark"
app:bsb_seek_by_section="true" />
步骤3-由于我需要自定义选项,所以我在onCreate上初始化了string.xml上声明的值数组。
bubbleSeekBar.setCustomSectionTextArray { sectionCount, array ->
array.clear()
for ((index, value) in resources.getStringArray(R.array.xxxx)
.withIndex()) {
array.put(index, value)
}
array
}
步骤4-您可以使用下面的方法捕获更改或设置值。
bubbleSeekBar.onProgressChangedListener
bubbleSeekBar.setProgress()
利卜很好,而且为我工作。要获得更多信息,请查看这个答案顶部的链接。
发布于 2020-05-19 16:41:45
你最好用SeekBar
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:thumb="@drawable/ic_location"
android:theme="@style/Widget.AppCompat.SeekBar.Discrete" />
发布于 2020-08-14 02:54:14
您可以使用一个SeekBar
并定义值的范围,为了删除平滑/精炼滚动,将OnSeekBarChangeListener
设置为它,并通过重新设置进度(如下图所示)使用需要的SeekBar
值的有效范围覆盖onProgressChanged()
。
示例
使用最大值为100的SeekBar
,步骤为10;因此,总有效值为10。
SeekBar seekBar = findViewById(R.id.seekbar);
seekBar.setMax(100);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean
fromUser) {
seekBar.setProgress((progress / 10) * 10);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
https://stackoverflow.com/questions/61896188
复制相似问题