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

Sun*_*nny 2 user-interface android seekbar

我正在做的事情:

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

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

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

Luk*_*rog 5

我想在Android应用程序中实现搜索栏.但是在搜索栏上我还想显示最大值和最小值.最小值始终为0,但最大值取决于剪辑长度.(例如:0 --- 180)

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

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

一个更简单的方法是使一个自定义组件SeekBar包含TextView左边和右边的一个(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);
    }

}
Run Code Online (Sandbox Code Playgroud)

内容布局在哪里:

<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" />
Run Code Online (Sandbox Code Playgroud)

当前的文本往往比它应该更加令人满意,并且需要额外的工作才能将它放在搜索句柄下.