自定义首选项,targetSdkVersion=“11":缺少缩进? [英] Custom preference, targetSdkVersion="11": missing indent?
问题描述
我有几个自定义的 DialogPreference
实现,例如 以及一个演示问题的简单首选项 XML 文件.即使只有两个 Java 类、首选项 XML 和一个 arrays.xml
文件,我也会遇到这种现象.这是来自该项目的编译 APK.
(交叉发帖自 关联的安卓开发者线程)
好的,我想通了.
Preference 有三种可能的构造函数:
MyPreference(Context ctxt)MyPreference(上下文 ctxt,AttributeSet attrs)MyPreference(上下文 ctxt,AttributeSet attrs,int defStyle)
沿着这条线的某个地方,我找到了拥有一参数构造函数链到二参数构造函数(为第二个参数传递 null
),并具有两个参数构造函数链到三参数构造函数(为第三个参数).
这不是正确的答案.
我希望正确的答案是只实施第二个构造函数,因为正确的默认样式是 Android 内部的(com.android.internal.R.attr.dialogPreferenceStyle
).第二构造函数是用于膨胀首选项 XML 的构造函数.
感谢大家的帮助!
I have a couple of custom DialogPreference
implementations floating around, such as this one:
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);
}
}
They work just fine. However, in an application with android:targetSdkVersion="11"
defined, on a XOOM, they show up missing the indent when in the PreferenceActivity
:
Also, the font size appears a smidge bigger, at least for the title.
There's nothing in DialogPreference
where I am really overriding any formatting behavior for that stuff, AFAIK. The preference XML is unremarkable, other than referring to the above class:
<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>
Anyone know where I'm going wrong?
Thanks!
UPDATE
Here is a link to a project that contains this custom preference and a simple preference XML file that demonstrates the problem. Even with just two Java classes, the preference XML, and an arrays.xml
file, I get this phenomenon. Here is a compiled APK from this project.
(cross-posting from the associated android-developers thread)
OK, I figured it out.
There are three possible constructors on a Preference:
MyPreference(Context ctxt)
MyPreference(Context ctxt, AttributeSet attrs)
MyPreference(Context ctxt, AttributeSet attrs, int defStyle)
Somewhere along the line, I picked up the pattern of having the
one-parameter constructor chain to the two-parameter constructor
(passing null
for the 2nd parameter), and having the two-parameter
constructor chain to the three-parameter constructor (passing 0 for
the 3rd parameter).
And that's not the right answer.
I am hoping that the right answer is to only implement the second
constructor, because the correct default style is internal to Android
(com.android.internal.R.attr.dialogPreferenceStyle
). The second
constructor is the one used with inflating preference XML.
Thanks to all for the help!
这篇关于自定义首选项,targetSdkVersion=“11":缺少缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!