Com*_*are 18 android android-preferences android-3.0-honeycomb
我有几个自定义DialogPreference实现,例如这个:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
他们工作得很好.但是,在具有已android:targetSdkVersion="11"定义的应用程序中,在XOOM上,它们在以下时显示缺少缩进PreferenceActivity:

此外,字体大小显得更大,至少对于标题而言.
没有什么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>
Run Code Online (Sandbox Code Playgroud)
谁知道我哪里出错了?
谢谢!
UPDATE
以下是包含此自定义首选项的项目的链接以及演示此问题的简单首选项XML文件.即使只有两个Java类,首选项XML和arrays.xml文件,我也会遇到这种现象.这是来自这个项目的编译APK.
Com*_*are 13
(从相关的android-developers线程交叉发布)
好的,我明白了.
Preference上有三种可能的构造函数:
MyPreference(Context ctxt)
MyPreference(Context ctxt, AttributeSet attrs)
MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
Run Code Online (Sandbox Code Playgroud)
在某个地方,我选择了将单参数构造函数链赋予双参数构造函数的模式(传递null第二个参数),并将双参数构造函数链链接到三参数构造函数(传递0表示第三个参数).
这不是正确的答案.
我希望正确的答案是只实现第二个构造函数,因为正确的默认样式是Android(com.android.internal.R.attr.dialogPreferenceStyle)内部的.第二个构造函数是用于膨胀首选项XML的构造函数.
感谢大家的帮助!
您可以使用void Preference.setWidgetLayoutResource(int widgetLayoutResId)方法跳舞,但我更喜欢View Preference.onCreateView(ViewGroup parent)在我的自定义Preference类中覆盖方法,并通过在下面添加自定义视图来破解它@android:id/summary(使用hierarchyviewer实用程序获取详细信息).
完整的方法是:
@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;
}
Run Code Online (Sandbox Code Playgroud)
可以在此处下载基于http://robobunny.com代码的SeekBarPreference类的源代码

| 归档时间: |
|
| 查看次数: |
4715 次 |
| 最近记录: |