2

私がやろうとしていること:

  1. SeekBarAndroidアプリにを実装したいのですが、その上SeekBarに最大値と最小値も表示したいと思います。最小値は常に0ですが、最大値はクリップの長さに依存します。(例:0 --- 180)

  2. ユーザーが移動したときに選択された値を(シークバー自体に)表示する方法はありますSeekBarか?

SeekBar値を表示するために利用できるオプションがないように思われるため、Androidと一緒にこれを実装する方法を理解できません。

4

1 に答える 1

5

Androidアプリにシークバーを実装したいと思います。しかし、シークバーには、最大値と最小値も表示したいと思います。最小値は常に0ですが、最大値はクリップの長さに依存します。(例:0 --- 180)

ユーザーがシークバーを移動したときに選択された値を(シークバー自体に)表示する方法はありますか?

これらの値を上に置きたい場合はSeekBar、クラスを拡張しSeekBarてメソッドをオーバーライドしますonDraw。電話super.onDraw(canvas)をかけた後、開始時と終了時の最小値/最大値、SeekBarまたは現在の進行状況など、独自のものを描画できます。テキストをどこにどのように描くかを慎重に計算する必要があるため、見栄えのするものを作成することは(そこにあるすべての異なるSeekbars点で)少し​​難しいものになります。

より簡単なアプローチは、カスタムコンポーネントを左右にSeekBaraでラップし(現在の進行状況については以下のオプションを使用)、最小/最大値でそれらを設定することです(最大値がプログラムで設定されている場合でも、最大それらの変更を「フォロー」するように作成することができます)。進行状況は、の幅と現在の進行状況を知ることで簡単に計算および更新できます。TextViewTextViewTextViewSeekBar

2番目のケースの小さな例:

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" />

現在のテキストは必要以上にオフセットされる傾向があり、シークハンドルのすぐ下に配置するには追加の作業が必要です。

于 2013-03-13T11:49:21.920 に答える