Android 如何在SeekBar上显示最大值和最小值?
我想做的是:Android 如何在SeekBar上显示最大值和最小值?,android,user-interface,seekbar,Android,User Interface,Seekbar,我想做的是: 我想在Android应用程序中实现一个SeekBar,在该SeekBar上,我还想显示最大值和最小值。“最小值”始终为0,但“最大值”取决于剪辑长度。(示例:0--180) 当用户移动SeekBar时,是否有方法显示所选的值(在搜索栏上) 我不知道如何在安卓SeekBar上实现这一点,因为似乎没有任何显示值的选项 我想在Android应用程序中实现seekbar。但是在 seekbar I还希望显示最大值和最小值。最小值 将始终为0,但最大值取决于剪辑长度。(例如: 0--180)
SeekBar
,在该SeekBar
上,我还想显示最大值和最小值。“最小值”始终为0,但“最大值”取决于剪辑长度。(示例:0--180)SeekBar
时,是否有方法显示所选的值(在搜索栏上)SeekBar
上实现这一点,因为似乎没有任何显示值的选项
我想在Android应用程序中实现seekbar。但是在
seekbar I还希望显示最大值和最小值。最小值
将始终为0,但最大值取决于剪辑长度。(例如:
0--180)
当用户>移动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" />
当前文本的偏移量往往超过其应有的偏移量,需要进行额外的工作才能将其放在seek句柄的正下方。非常感谢。请提供使用seekbar左右两侧的testview的示例dialog@Sunny请看我编辑过的答案,记住这是一个非常基本的问题如果希望它介于max和min之间,则需要执行
final int seekLocation=(((newProgress mMin)*totalSeekWidth)/(mMax mMin))-(mCurrentText.getWidth());