2011-04-22 100 views
18

我有一對夫婦定製DialogPreference實現左右浮動,如this one的:自定義首選項,targetSdkVersion =「11」:缺少縮進?

package apt.tutorial; 

import android.content.Context; 
import android.content.res.TypedArray; 
import android.os.Bundle; 
import android.os.Parcel; 
import android.os.Parcelable; 
import android.preference.DialogPreference; 
import android.util.AttributeSet; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewParent; 
import android.widget.TimePicker; 

public class TimePreference extends DialogPreference { 
    private int lastHour=0; 
    private int lastMinute=0; 
    private TimePicker picker=null; 

    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("Set"); 
     setNegativeButtonText("Cancel"); 
    } 

    @Override 
    protected View onCreateDialogView() { 
     picker=new TimePicker(getContext()); 

     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) { 
      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:targetSdkVersion="11"應用程序定義,在XOOM,他們表現出了缺少的縮進PreferenceActivity時:

PreferenceActivity with messed-up custom DialogPreference

此外,字體大小出現微幅下挫較大,至少爲標題。

DialogPreference沒有什麼東西可以覆蓋任何格式化行爲,AFAIK。偏好的XML是不起眼的,除了提到上面的類:

<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <ListPreference 
     android:key="sort_order" 
     android:title="Sort Order" 
     android:summary="Choose the order the list uses" 
     android:entries="@array/sort_names" 
     android:entryValues="@array/sort_clauses" 
     android:dialogTitle="Choose a sort order" /> 
    <CheckBoxPreference 
     android:key="alarm" 
     android:title="Sound a Lunch Alarm" 
     android:summary="Check if you want to know when it is time for lunch" /> 
    <apt.tutorial.TimePreference 
     android:key="alarm_time" 
     android:title="Lunch Alarm Time" 
     android:defaultValue="12:00" 
     android:summary="Set your desired time for the lunch alarm" 
     android:dependency="alarm" /> 
    <CheckBoxPreference 
     android:key="use_notification" 
     android:title="Use a Notification" 
     android:defaultValue="true" 
     android:summary="Check if you want a status bar icon at lunchtime, or uncheck for a full-screen notice" 
     android:dependency="alarm" /> 
</PreferenceScreen> 

任何人都知道我要去哪裏錯了?

謝謝!


UPDATE

Here is a link to a project包含此定製偏好和演示該問題的簡單偏好的XML文件。即使只有兩個Java類,首選項XML和一個arrays.xml文件,我也會遇到這種現象。 Here is a compiled APK來自這個項目。

+0

你應該去問問馬克·墨菲 - 他什麼都知道:) – JohnnyLambada 2011-04-22 23:03:06

+1

@JohnnyLambada:我試過了,但綠灣包裝工隊前線辦公室拒絕發表評論。等一下。你不是指http://goo.gl/KNYOu? :-) – CommonsWare 2011-04-22 23:49:04

+0

與馬克墨菲有同樣的問題總是很好的。如果他不能很容易地弄清楚,那麼我並不覺得那麼糟糕。 – Intrications 2012-09-01 01:10:23

回答

12

(從associated android-developers thread交叉發佈)

OK,我想它了。

上有一個偏好三種可能的構造:

MyPreference(Context ctxt) 
MyPreference(Context ctxt, AttributeSet attrs) 
MyPreference(Context ctxt, AttributeSet attrs, int defStyle) 

某處沿着線,我拿起具有 一個參數的構造鏈的兩個參數的構造 的圖案(通過null爲第二個參數),並且具有雙參數構造函數鏈給三參數構造函數(第三個參數爲 傳遞0)。

這不是正確的答案。

我希望,正確的答案是隻實施第二 構造,因爲正確的默認樣式是內部到Android (com.android.internal.R.attr.dialogPreferenceStyle)。第二個 構造函數是與膨脹偏好XML一起使用的構造函數。

感謝大家的幫助!

+0

解決方案是簡單地將「com.android.internal.R.attr.dialogPreferenceStyle」而不是0作爲傳遞給第三個構造函數的第三個屬性?我遇到了同樣的問題,並最終放棄了它。 – Tenfour04 2011-07-31 17:40:37

+0

@ TenFour04:您無權訪問'com.android.internal.R.attr.dialogPreferenceStyle'。 – CommonsWare 2011-07-31 20:31:47

+0

那不好玩。我不明白爲什麼某些資源受到限制,而其他資源則不受限制。例如,您可以使用list_selector_background,但不能使用list_selector_holo_dark。謝謝。 – Tenfour04 2011-07-31 21:36:40

1

我在模擬器上試過了你的代碼。你所提供的代碼沒有問題,所有的行都有相同的格式;但他們都看起來比其他人更相似(格式)第三首選項(Lunch Alarm Time)。

它看起來像其他三個首選項縮進比需要更多。因此,也許您使用了一些全局格式化的樣式,但沒有選擇TimePreference首選項。

編輯:好的。所以,以上不是(完全)真實的。當我嘗試將目標sdk設置爲HoneyComb時肯定存在問題。但是,在將PreferenceActivity課程的主題設置爲android:theme="@android:style/Theme.Black"時,如下所示,所有首選項的外觀都保持一致。

enter image description here

這種風格類似於Froyo,但不是HoneyComb;在後者中,標題字體更小,縮進更多。可能的話,默認主題不會被分配到Custom Preferences - 只是一個猜測:)解決方法是顯式地將默認主題分配給您的首選項活動,但我不知道HoneyComb中的默認主題是什麼(以及它是否可以被設置)。

+0

@Rajath DSouza:我在3.0模擬器上獲得了與XOOM上相同的行爲。你嘗試了什麼模擬器? – CommonsWare 2011-04-23 12:26:34

+0

我試着用Target SDK設置爲Android 3.0(API level 11)'和Skin to'WXGA'的Android SDK模擬器。 – rajath 2011-04-23 12:31:54

+0

@Rajath DSouza:多麼奇怪。在這個特定的項目中我根本沒有任何風格,所以這看起來不像是問題的根源。我將不得不做一些更多的嘗試,並嘗試縮小問題的範圍。 – CommonsWare 2011-04-23 12:36:26

6

您可以void Preference.setWidgetLayoutResource(int widgetLayoutResId)方法跳舞,但我更願意將覆蓋我的自定義偏好類View Preference.onCreateView(ViewGroup parent)方法,並通過添加略低於@android:id/summary自定義視圖(使用hierarchyviewer瞭解詳細信息工具)破解它。

完整的方法是:從http://robobunny.com基於代碼我SeekBarPreference類的

@Override 
protected View onCreateView(ViewGroup parent) 
{ 
    View ret = super.onCreateView(parent); 

    View summary = ret.findViewById(android.R.id.summary); 
    if (summary != null) 
    { 
     ViewParent summaryParent = summary.getParent(); 
     if (summaryParent instanceof ViewGroup) 
     { 
      final LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      ViewGroup summaryParent2 = (ViewGroup) summaryParent; 
      layoutInflater.inflate(R.layout.seek_bar_preference, summaryParent2); 

      seekBar = (SeekBar) summaryParent2.findViewById(R.id.seekBar); 
      seekBar.setMax(maxValue - minValue); 
      seekBar.setOnSeekBarChangeListener(this); 

      statusText = (TextView) summaryParent2.findViewById(R.id.seekBarPrefValue); 

      unitsRightView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsRight); 
      unitsLeftView = (TextView) summaryParent2.findViewById(R.id.seekBarPrefUnitsLeft); 
     } 
    } 

    return ret; 
} 

源代碼可以下載here image1 image2

1

這幫助我解決辦法:

我已取代

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

public TimePreference(Context ctxt, AttributeSet attrs) { 
    this(ctxt, attrs, ctxt.getResources().getSystem().getIdentifier("dialogPreferenceStyle", "attr", "android")); 
} 

正如你所看到的,我換成第三個參數與ctxt.getResources()getSystem()則getIdentifier( 「dialogPreferenceStyle」, 「ATTR」, 「機器人」。 )在自定義首選項類的第二個構造函數中。

+0

謝謝。這對我非常有幫助。 – Dalinaum 2015-01-19 17:10:59

1

使接受的答案更清晰。你只需要這個構造:

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