私がやろうとしていること:
SeekBar
Androidアプリにを実装したいのですが、その上SeekBar
に最大値と最小値も表示したいと思います。最小値は常に0ですが、最大値はクリップの長さに依存します。(例:0 --- 180)ユーザーが移動したときに選択された値を(シークバー自体に)表示する方法はあります
SeekBar
か?
SeekBar
値を表示するために利用できるオプションがないように思われるため、Androidと一緒にこれを実装する方法を理解できません。
私がやろうとしていること:
SeekBar
Androidアプリにを実装したいのですが、その上SeekBar
に最大値と最小値も表示したいと思います。最小値は常に0ですが、最大値はクリップの長さに依存します。(例:0 --- 180)
ユーザーが移動したときに選択された値を(シークバー自体に)表示する方法はありますSeekBar
か?
SeekBar
値を表示するために利用できるオプションがないように思われるため、Androidと一緒にこれを実装する方法を理解できません。
Androidアプリにシークバーを実装したいと思います。しかし、シークバーには、最大値と最小値も表示したいと思います。最小値は常に0ですが、最大値はクリップの長さに依存します。(例:0 --- 180)
ユーザーがシークバーを移動したときに選択された値を(シークバー自体に)表示する方法はありますか?
これらの値を上に置きたい場合はSeekBar
、クラスを拡張しSeekBar
てメソッドをオーバーライドしますonDraw
。電話super.onDraw(canvas)
をかけた後、開始時と終了時の最小値/最大値、SeekBar
または現在の進行状況など、独自のものを描画できます。テキストをどこにどのように描くかを慎重に計算する必要があるため、見栄えのするものを作成することは(そこにあるすべての異なるSeekbars
点で)少し難しいものになります。
より簡単なアプローチは、カスタムコンポーネントを左右にSeekBar
aでラップし(現在の進行状況については以下のオプションを使用)、最小/最大値でそれらを設定することです(最大値がプログラムで設定されている場合でも、最大それらの変更を「フォロー」するように作成することができます)。進行状況は、の幅と現在の進行状況を知ることで簡単に計算および更新できます。TextView
TextView
TextView
SeekBar
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" />
現在のテキストは必要以上にオフセットされる傾向があり、シークハンドルのすぐ下に配置するには追加の作業が必要です。