android - 如何在 SeekBar 上显示最大值和最小值?

标签 android user-interface seekbar

我正在尝试做的事情:

  1. 我想在 Android 应用程序中实现 SeekBar,并在该 SeekBar 上显示最大值和最小值。最小值始终为 0,但最大值取决于剪辑长度。 (示例:0---180)

  2. 有没有办法显示用户移动 SeekBar 时选择的值(在搜索栏本身上)?

我无法弄清楚如何与 android SeekBar 一起实现它,因为似乎没有任何可用于显示值的选项。

最佳答案

I would like to implement a seekbar in an Android App. But on the seekbar I would also like to display the max and min values. Min value would always be 0 but max value is dependent on clip length. (Example: 0---180)

Is there a way to display the value (on the seek bar itself) that is selected when the user > moves the seekbar?

如果您希望这些值位于 SeekBar顶部,请扩展 SeekBar 类并覆盖 onDraw方法。调用 super.onDraw(canvas) 后,您可以绘制自己的内容,例如 SeekBar 开始和结束处的最小/最大值或当前进度。制作看起来不错的东西(在所有不同的 Seekbars 上)会有点困难,因为您需要仔细计算在哪里以及如何绘制文本。

更简单的方法是制作一个自定义组件,在其左侧和右侧使用 TextView 包装 SeekBar(带有可选的 TextView下面列出了当前进度)并设置最小/最大值(即使以编程方式设置最大值,也可以使最大 TextView 来“跟随”这些更改)。知道 SeekBar 的宽度和当前进度,可以轻松计算和更新进度。

第二种情况的小例子:

public static class SeekBarWithValues extends RelativeLayout {

    private int mMax = 100;
    private TextView mMinText;
    private TextView mMaxText;
    private TextView mCurrentText;
    private SeekBar mSeek;

    public SeekBarWithValues(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(getContext()).inflate(
                R.layout.content, this);
        // the minimum value is always 0
        mMinText = (TextView) findViewById(R.id.minValue);
        mMinText.setText("0");
        mMaxText = (TextView) findViewById(R.id.maxValue);
        mCurrentText = (TextView) findViewById(R.id.curentValue);
        mSeek = (SeekBar) findViewById(R.id.seekBar);
        mSeek.setMax(100);
        mMaxText.setText(String.valueOf(mSeek.getMax()));
    }

    /**
     * This needs additional work to make the current progress text stay
     * right under the thumb drawable.
     * 
     * @param newProgress
     *            the new progress for which to place the text
     */
    public void updateCurrentText(int newProgress) {
        mCurrentText.setText(String.valueOf(newProgress));
        final int padding = mMinText.getWidth() + mSeek.getPaddingLeft();
        final int totalSeekWidth = mSeek.getWidth();
        final RelativeLayout.LayoutParams lp = (LayoutParams) mCurrentText
                .getLayoutParams();
        final int seekLocation = (mSeek.getProgress() * totalSeekWidth)
                / mMax - mCurrentText.getWidth() / 2;
        lp.leftMargin = seekLocation + padding;
        mCurrentText.setLayoutParams(lp);
    }

    public SeekBar getSeekBar() {
        return mSeek;
    }

    public void updateSeekMaxValue(int newValue) {
        mMax = newValue;
        mMaxText.setText(mMax);
        mSeek.setMax(mMax);
    }

}

内容布局在哪里:

<merge xmlns:android="http://schemas.android.com/apk/res/android" >

<TextView
    android:id="@+id/minValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/seekBar"
    android:layout_alignParentLeft="true"
    android:layout_alignTop="@id/seekBar"
    android:gravity="center" />

<TextView
    android:id="@+id/maxValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@id/seekBar"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/seekBar"
    android:gravity="center" />

<SeekBar
    android:id="@id/seekBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@id/maxValue"
    android:layout_toRightOf="@id/minValue" />

<TextView
    android:id="@+id/curentValue"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@id/seekBar"
    android:gravity="center" />

当前文本的偏移量往往超出应有的范围,需要进行额外的工作才能将其放在搜索句柄的正下方。

关于android - 如何在 SeekBar 上显示最大值和最小值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15383623/

相关文章:

java - Android:如何不保存实例状态?

android - 不能使用 MediaStore.Images.Media.insertImage

java - JSON 到 GSON 对象

android - 更新服务中的 MusicPlayer SeekBar

Android 如何从 Material Design 文档中实现 Bottom Sheet

java - 如何将 JButton 设置为未修饰并删除 MouseListener

HTML title 属性而不是标签内容

python - tkinter 调用 tk.canvas.create_window() 时什么是窗口

android - 如何在其他 Activity 中获得偏好

Android seekbar如何在短时间内阻止拖动/禁用它