如何停止我的首选项屏幕为自定义首选项提供不同的字体大小

How to stop my preference screen giving a different font size to a custom made preference

目前我的设置 activity 给了我两种字体大小。一种文本大小适用于所有预定义首选项,另一种文本大小适用于自定义首选项。首选项 xml 确实看起来像这样。从对话框首选项扩展的自定义首选项。

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <CheckBoxPreference
    android:key="changemode"
    android:title="@string/change_mode"
    android:defaultValue="true"
    android:summary="@string/change_mode_summary"
    />

<org.myapp.settings.NumberPickerPreference
    android:id="@+id/pref_num_picker_pref"
    android:key="@string/number_of_uploads"
    android:title="@string/number_of_uploads"
    />


</PreferenceScreen>

这是什么原因?由于 none 的首选项具有预定义的文本大小,是否可以在不添加 textsize 属性的情况下解决此问题?

如果您扩充或创建自己的(文本)视图,您应该确保它们具有与默认实现相同的样式。

this source layout 看来,他们使用 textAppearanceMedium 作为标题,使用 textAppearanceSmall 作为摘要。

因此,在标题的自定义布局中,您应该使用

<!-- for title -->
android:textAppearance="?android:attr/textAppearanceMedium"
<!-- for summary or description -->
android:textAppearance="?android:attr/textAppearanceSmall"

如果您仅以实用的方式创建视图(在这种情况下您可能不应该这样做),您可以使用

// for title
titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Medium);
// for summary
titleView.setTextAppearance(android.R.style.TextAppearance_DeviceDefault_Small);

在自定义对话框(扩展 DialogPreference 的对话框)的 class 的构造函数中,您必须指定

R.attr.dialogPreferenceStyle

例如:

public class YourPreference extends DialogPreference {

    public YourPreference(Context context, AttributeSet attrs) {
        this(context, attrs, R.attr.dialogPreferenceStyle);
    }
 (...) 
}

添加到@MichaelZet 的回答中,我遇到了与 PreferenceScreen 元素类似的情况,该元素具有 3 个子元素:switchPreference、List Preference 和 customTimePreference。 然而,customTimePreference 是自定义 class 扩展 DialogPreference。问题是前 2 个条目(switchPreference、ListPreference)有它们的标题,summary textSize small/same 但 customTimePreference 的标题与前两个元素相比很大。

问题是,我的 customTimePreference 的超级构造函数(DialogPreference 构造函数)有一个 defStyleAttr 参数,它是 PreferenceScreen 的子元素(第 3 个屏幕项)和单击的弹出对话框 dialogPreference(customTimePreference) 的默认样式,我正在通过该属性的值为 0,表示不按照下面显示的 https://whosebug.com/a/6784919/11971304 给出的答案查找默认值。

defStyleAttr 当前主题中的一个属性,它包含对为 StyledAttributes 提供默认值的样式资源的引用。可以为 0 以不查找默认值。

defStyleRes 为 StyledAttributes 提供默认值的样式资源的资源标识符,仅当 defStyleAttr 为 0 或在主题中找不到时才使用。可以为 0 以不查找默认值。

传入 android.R.attr.dialogPreferenceStyle 使其使用 PrefrenceScreen 上的默认样式并符合其他元素标题大小的外观。

 <PreferenceScreen android:title="Settings">
        <SwitchPreference
            android:defaultValue="..."
            android:key="..."
            android:summary="Switch Summary"
            android:title="Enable Switch" />
        <ListPreference
            android:dialogTitle="Please Select"
            android:entries="@array/list_ids"
            android:entryValues="@array/list_values"
            android:key="..."
            android:title="Selection List popup" />

        <com.android.example.customTimePreference
            android:key="..."
            android:title="Clock Dialog popup"/>
    </PreferenceScreen>
public TimePreference(Context context) {
        this(context, null);
        this.context = context;
    }

    public TimePreference(Context context, AttributeSet attrs) {
        //this(context, attrs, 0);       //default not supplied causing incorrect textSize
        this(context, attrs, android.R.attr.dialogPreferenceStyle);
        this.context = context;
    }

    public TimePreference(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.picker = null;
        setPositiveButtonText(R.string.ok);
        setNegativeButtonText(R.string.cancel);
        this.calendar = new GregorianCalendar();
        this.context = context;
}