我寫了一個自定義首選項類,其中包含一個簡單的進度欄,以允許設置音量等。 偏好效果很好,直到我嘗試將這兩個偏好設置放入我的首選項屏幕中。自定義首選項跳過屏幕
當我這樣做時,首選項屏幕開始變得很粗糙»當我拖動搜索欄時,兩個自定義首選項會不斷切換位置。
任何人有任何想法爲什麼發生這種情況? 感謝
編輯: 這裏是習俗偏好的代碼
import android.content.Context; import
android.content.SharedPreferences; import android.content.res.TypedArray; import android.graphics.Typeface; import android.preference.Preference; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.TextView;
public class SeekBarPreference extends Preference implements OnSeekBarChangeListener {
private int mMaximum = 60; private int mInterval = 1; private int mMinimum = 1;
private int mDefault = 50; private TextView monitorBox;
public SeekBarPreference(Context context) { super(context); }
public SeekBarPreference(Context context, AttributeSet attrs) { super(context, attrs); initValues(context, attrs); }
public SeekBarPreference(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initValues(context, attrs); }
private void initValues(Context context, AttributeSet attrs) { TypedArray styledAttributes = context.obtainStyledAttributes(attrs,
R.styleable.SeekBarPreference); mMinimum = styledAttributes.getInt(
R.styleable.SeekBarPreference_barMinValue, 1); mMaximum = styledAttributes.getInt(
R.styleable.SeekBarPreference_barMaxValue, 1); mInterval = styledAttributes.getInt(
R.styleable.SeekBarPreference_barInterval, 1); mDefault = styledAttributes.getInt(
R.styleable.SeekBarPreference_defaultValue, mMaximum); }
@Override protected View onCreateView(ViewGroup parent) {
LinearLayout horizontalLayout = new LinearLayout(getContext()); horizontalLayout.setPadding(15, 5, 10, 5); horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout verticalLayout = new LinearLayout(getContext()); verticalLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT); layoutParams.gravity = Gravity.LEFT; layoutParams.weight = 0.5f;
LinearLayout.LayoutParams settingTitleLayoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT); settingTitleLayoutParams.gravity = Gravity.LEFT; settingTitleLayoutParams.weight =
1.0f;
LinearLayout.LayoutParams seekbarLayoutParams = new LinearLayout.LayoutParams(
80, LinearLayout.LayoutParams.WRAP_CONTENT); seekbarLayoutParams.gravity = Gravity.RIGHT | Gravity.CENTER_VERTICAL;
LinearLayout.LayoutParams selectedAmountLayoutParams = new LinearLayout.LayoutParams(
30, LinearLayout.LayoutParams.WRAP_CONTENT); selectedAmountLayoutParams.gravity = Gravity.CENTER;
TextView titleTextView = new TextView(getContext()); titleTextView.setText(getTitle()); titleTextView.setTextSize(18); titleTextView.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD); titleTextView.setGravity(Gravity.LEFT); titleTextView.setLayoutParams(settingTitleLayoutParams);
TextView summeryTextView = new TextView(getContext()); summeryTextView.setText(getSummary()); summeryTextView.setTextSize(14); summeryTextView.setTypeface(Typeface.SANS_SERIF); summeryTextView.setGravity(Gravity.LEFT); summeryTextView.setLayoutParams(settingTitleLayoutParams);
SeekBar bar = new SeekBar(getContext()); bar.setMax(mMaximum); bar.setProgress(mDefault); bar.setPadding(10, 0, 10, 0); bar.setLayoutParams(seekbarLayoutParams); bar.setOnSeekBarChangeListener(this);
this.monitorBox = new TextView(getContext()); this.monitorBox.setTextSize(12); this.monitorBox.setTypeface(Typeface.MONOSPACE, Typeface.ITALIC); this.monitorBox.setLayoutParams(selectedAmountLayoutParams); this.monitorBox.setPadding(2, 5, 0, 0); this.monitorBox.setText(bar.getProgress()
+ "");
verticalLayout.addView(titleTextView); verticalLayout.addView(summeryTextView); horizontalLayout.addView(verticalLayout, layoutParams); horizontalLayout.addView(bar); horizontalLayout.addView(this.monitorBox); horizontalLayout.setId(android.R.id.widget_frame);
return horizontalLayout; }
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = Math.round(((float) progress)/mInterval) * mInterval; if (progress < mMinimum) { progress = mMinimum; }
if (!callChangeListener(progress)) { seekBar.setProgress((int) this.mDefault); return; }
seekBar.setProgress(progress); this.mDefault = progress; this.monitorBox.setText(progress + ""); updatePreference(progress);
notifyChanged(); }
public void onStartTrackingTouch(SeekBar seekBar) { }
public void onStopTrackingTouch(SeekBar seekBar) { }
@Override protected Object onGetDefaultValue(TypedArray ta, int index) {
int dValue = (int) ta.getInt(index, 5);
return validateValue(dValue); }
@Override protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { int temp = restoreValue ? getPersistedInt(5) : (Integer) defaultValue;
if (!restoreValue) persistInt(temp);
this.mDefault = temp; }
private int validateValue(int value) {
if (value > mMaximum) value = mMaximum; else if (value < 0) value = 0; else if (value % mInterval != 0) value = Math.round(((float) value)/mInterval) * mInterval;
return value; }
private void updatePreference(int newValue) {
SharedPreferences.Editor editor = getEditor(); editor.putInt(getKey(), newValue); editor.commit(); }
}
編輯:我發現,創造與excat相同的代碼,但不同名稱的第二類每一個使用一個解決問題=有史以來最糟糕的一次。我不想這樣做。任何人有任何想法?
發佈代碼! – Nate 2010-10-13 22:11:31
在這裏發佈的文件更容易redabilty:http://jump.fm/EUNTP – Jason 2010-10-15 15:00:17