
>我想在Android应用程序中实现SeekBar,在SeekBar上我还要显示最大值和最小值.最小值始终为0,但最大值取决于剪辑长度. (例如:0 — 180)
>有没有办法显示用户移动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上)将会有点困难,因为您需要仔细计算绘制文本的位置和方式.
一个更简单的方法是使一个自定义组件包含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);
}
}
内容布局在哪里:
<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上显示最大值和最小值? - 乐贴网