如何在搜索栏显示的最大值和最小值? [英] How to display max and min values on a SeekBar?
问题描述
我想要做的:
-
我想在一个Android应用程序,并在该实施
搜索栏
搜索栏
我也要要显示的最大值和最小值。最小值将永远是0,但最大值依赖于剪辑的长度。 (例如:0 --- 180) -
有没有显示值(在搜索栏本身)所选择的用户移动时的方式
搜索栏
我不能够弄清楚,如何实现这与Android 搜索栏
一起,因为似乎没有可用于显示值的任何选项。
我想实现在Android应用程序一个搜索栏。但在
搜索栏我也想显示最大值和最小值。最小值
将永远是0,但最大值依赖于剪辑的长度。 (例:
0 --- 180)
有没有显示值(在搜索栏本身)被选中,当用户>移动搜索栏?一个办法
块引用>如果您希望这些值顶部的
搜索栏
然后扩展搜索栏
类和重写的onDraw
方法。打完电话后super.onDraw(画布)
然后你可以绘制自己的东西,比如在开始和结束的最小/最大值搜索栏
或目前的进展。制作的东西,看起来不错(在所有不同的期待Seekbars
在那里),将东西有点更难,因为你需要仔细计算在何处和如何绘制文本。有一个简单的方法是做一个自定义组件包裹
搜索栏
与的TextView
在它的左,右(带有可选的的TextView
低于目前的进展),并设置那些用最小/最大值(即使最大值设置编程,最大的TextView
可以作出跟随这些变化)。进度可以很容易地计算和更新知道的宽度搜索栏
和目前的进展。对于第二种情况一个小例子:
公共静态类SeekBarWithValues扩展RelativeLayout的{ 私人诠释MMAX = 100;
私人TextView的mMinText;
私人TextView的mMaxText;
私人TextView的mCurrentText;
私人搜索栏mSeek; 公共SeekBarWithValues(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
LayoutInflater.from(的getContext())。膨胀(
R.layout.content,这一点);
//的最小值总是0
mMinText =(的TextView)findViewById(R.id.minValue);
mMinText.setText(0);
mMaxText =(的TextView)findViewById(R.id.maxValue);
mCurrentText =(的TextView)findViewById(R.id.curentValue);
mSeek =(搜索栏)findViewById(R.id.seekBar);
mSeek.setMax(100);
mMaxText.setText(将String.valueOf(mSeek.getMax()));
} / **
*这需要额外的工作,使目前的进度文本住宿
*拇指绘下的权利。
*
* @参数newProgress
*要为其放置文本的新进展
* /
公共无效updateCurrentText(INT newProgress){
mCurrentText.setText(将String.valueOf(newProgress));
最终诠释填充= mMinText.getWidth()+ mSeek.getPaddingLeft();
最终诠释totalSeekWidth = mSeek.getWidth();
最后RelativeLayout.LayoutParams LP =(的LayoutParams)mCurrentText
.getLayoutParams();
最终诠释seekLocation =(mSeek.getProgress()* totalSeekWidth)
/ MMAX - mCurrentText.getWidth()/ 2;
lp.leftMargin = seekLocation +填充;
mCurrentText.setLayoutParams(LP);
} 公共搜索栏getSeekBar(){
返回mSeek;
} 公共无效updateSeekMaxValue(INT newValue)以{
MMAX =为newValue;
mMaxText.setText(MMAX);
mSeek.setMax(MMAX);
}}凡内容布局是:
<合并的xmlns:机器人=http://schemas.android.com/apk/res/android><的TextView
机器人:ID =@ + ID / minValue(最小值)
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ + ID /搜索栏
机器人:layout_alignParentLeft =真
机器人:layout_alignTop =@ ID /搜索栏
机器人:比重=中心/><的TextView
机器人:ID =@ + ID /包括maxValue
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignBottom =@ ID /搜索栏
机器人:layout_alignParentRight =真
机器人:layout_alignTop =@ ID /搜索栏
机器人:比重=中心/><搜索栏
机器人:ID =@ ID /搜索栏
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_toLeftOf =@ ID /包括maxValue
机器人:layout_toRightOf =@ ID / minValue(最小值)/><的TextView
机器人:ID =@ + ID / curentValue
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_below =@ ID /搜索栏
机器人:比重=中心/>
目前的案文往往被offseted超过它应该和额外的工作,把它右侧的手柄寻求下是必需的。
What I'm trying to do:
I would like to implement a
SeekBar
in an Android App and on thatSeekBar
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
?I am not being able to figure out, how to implement this along with the android
SeekBar
, as there doesn't seem to be any option available for displaying values.解决方案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?
If you want those values on top of the
SeekBar
then extend theSeekBar
class and override theonDraw
method. After callingsuper.onDraw(canvas)
you can then draw your own stuff, like the min/max values at the start and end of theSeekBar
or the current progress. Making something that looks good(on all the different lookingSeekbars
out there) will be something a bit harder as you'll need to carefully calculate where and how you draw the text.A simpler approach would be to make a custom component wrapping the
SeekBar
with aTextView
on it's left and right(with an optionalTextView
below for the current progress) and set those with the min/max values(even if the max values is set programmatically, the maxTextView
could be made to "follow" those changes). The progress can be easily calculated and updated knowing the width of theSeekBar
and the current progress.A small example for the second case:
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); } }
Where the content layout is:
<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" />
The current text tends to be offseted more than it should and additional work is required to put it right under the seek handle.
这篇关于如何在搜索栏显示的最大值和最小值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!