2012-12-28 60 views
1

對於自定義偏好設置,例如一時間選擇器偏好,我用下面的XML在安卓Android中使用正確佈局的自定義偏好設置

<com.my.package.TimePreference android:key="notification_time" android:selectable="true" android:title="@string/notification_time" android:enabled="true" android:summary="@string/setTime" android:defaultValue="00:00" /> 

凡類「TimePreference」是:

package com.my.package; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.preference.DialogPreference; 
import android.text.format.DateFormat; 
import android.util.AttributeSet; 
import android.view.View; 
import android.widget.TimePicker; 

public class TimePreference extends DialogPreference { 

    private int lastHour = 0; 
    private int lastMinute = 0; 
    private TimePicker picker = null; 
    private boolean is_24_hours = true; 

    public static int getHour(String time) { 
     String[] pieces = time.split(":"); 
     return(Integer.parseInt(pieces[0])); 
    } 

    public static int getMinute(String time) { 
     String[] pieces = time.split(":"); 
     return(Integer.parseInt(pieces[1])); 
    } 

    public TimePreference(Context ctxt) { 
     this(ctxt, null); 
    } 

    public TimePreference(Context ctxt, AttributeSet attrs) { 
     this(ctxt, attrs, 0); 
    } 

    public TimePreference(Context ctxt, AttributeSet attrs, int defStyle) { 
     super(ctxt, attrs, defStyle); 
     setPositiveButtonText(ctxt.getString(R.string.save)); 
     setNegativeButtonText(ctxt.getString(R.string.cancel)); 
     try { 
      is_24_hours = DateFormat.is24HourFormat(ctxt); 
     } 
     catch (Exception e) { 
      is_24_hours = true; 
     } 
    } 

    @Override 
    protected View onCreateDialogView() { 
     picker = new TimePicker(getContext()); 
     if (is_24_hours) { 
      picker.setIs24HourView(true); 
     } 
     else { 
      picker.setIs24HourView(false); 
     } 
     return(picker); 
    } 

    @Override 
    protected void onBindDialogView(View v) { 
     super.onBindDialogView(v); 
     picker.setCurrentHour(lastHour); 
     picker.setCurrentMinute(lastMinute); 
    } 

    @Override 
    protected void onDialogClosed(boolean positiveResult) { 
     super.onDialogClosed(positiveResult); 
     if (positiveResult) { 
      picker.clearFocus(); // important - otherwise manual input is not saved 
      lastHour = picker.getCurrentHour(); 
      lastMinute = picker.getCurrentMinute(); 
      String time = String.valueOf(lastHour)+":"+String.valueOf(lastMinute); 
      if (callChangeListener(time)) { 
       persistString(time); 
      } 
     } 
    } 

    @Override 
    protected Object onGetDefaultValue(TypedArray a, int index) { 
     return(a.getString(index)); 
    } 

    @Override 
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) { 
     String time = null; 
     if (restoreValue) { 
      if (defaultValue == null) { 
       time = getPersistedString("00:00"); 
      } 
      else { 
       time = getPersistedString(defaultValue.toString()); 
      } 
     } 
     else { 
      time = defaultValue.toString(); 
     } 
     lastHour = getHour(time); 
     lastMinute = getMinute(time); 
    } 
} 

這一直在Android 2.2和2.3偉大的工作 - 但現在,我「VE切換到Android 4.0,我可以看到這個選項不調整個人偏好屏幕的佈局:

enter image description here

哪有我那麼解決這個問題?是否有比手動設置邊距/填充更好的解決方案?

回答

4

終於發現了問題:

構造super()必須被稱爲是隻需要兩個參數(無int defStyle參數)。

在問題的代碼中,調用super()時,預定義的defStyle值爲0,這防止Android選擇任何漂亮的佈局。如果您在未給出默認樣式參數的情況下調用super(),超類DialogPreference的構造函數會自行選擇最佳樣式。