2013-03-13 319 views
2

我想要做的事:如何在SeekBar上显示最大值和最小值?

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

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

我不能够弄清楚,如何实现这种与Android SeekBar一起,因为似乎没有可用于显示值的任何选项。

回答

3

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

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

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

一个更简单的方法是创建一个自定义组件与TextView包裹SeekBar它的左右(以低于目前的进展可选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" /> 

当前文本往往比它应该和其他工作进行offseted更多需要把它放在查询句柄下。

+0

非常感谢。能否请你提供一个在seekbar对话框的左侧和右侧使用testview的示例 – Sunny 2013-03-13 13:28:42

+0

@Sunny看到我编辑的答案,记住这是非常基本的东西。 – Luksprog 2013-03-13 14:19:24

+0

我想补充一点,如果你想要它在max和min之间,你需要执行final int seekLocation =(((newProgress-mMin)* totalSeekWidth)/(mMax-mMin)) - (mCurrentText.getWidth ());' – 2014-08-13 03:38:26

相关问题