Android 在EditTextPreference对话框中更改PositiveButtonText上的文本大小

Android 在EditTextPreference对话框中更改PositiveButtonText上的文本大小,android,dialog,preference,preferencescreen,edittextpreference,Android,Dialog,Preference,Preferencescreen,Edittextpreference,我不知道如何更改EditTextPreference对话框中按钮上的文本大小。我希望我整个应用程序中的所有文本都能在24 sp时进行精简。除了首选项屏幕上的此对话框外,其他地方都会发生这种情况。正如您在ListPreference中看到的,我找到了如何隐藏positiveButtonText和negativeButtonText的方法 如果可能的话,我希望在xml中这样做。我还希望避免创建自定义对话框布局。在EditTextPreference标签中,我可以编辑对话框中的标题和布局,但我不知道如

我不知道如何更改EditTextPreference对话框中按钮上的文本大小。我希望我整个应用程序中的所有文本都能在24 sp时进行精简。除了首选项屏幕上的此对话框外,其他地方都会发生这种情况。正如您在ListPreference中看到的,我找到了如何隐藏positiveButtonText和negativeButtonText的方法

如果可能的话,我希望在xml中这样做。我还希望避免创建自定义对话框布局。在EditTextPreference标签中,我可以编辑对话框中的标题和布局,但我不知道如何“触摸”默认按钮

以下是我的pref_connection.xml:

<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="Connection">

    <ListPreference
        android:defaultValue="box1"
        android:entries="@array/pref_box_type_list_titles"
        android:entryValues="@array/pref_box_type_list_values"
        android:key="box_types_list"
        android:negativeButtonText="@null"
        android:positiveButtonText="@null"
        android:layout="@layout/preference_screen_layout"
        android:title="@string/pref_title_box_type"/>

    <EditTextPreference
        android:defaultValue="@string/pref_default_internet_addr"
        android:digits="0123456789."
        android:inputType="number|numberDecimal"
        android:key="internet_addr"
        android:selectAllOnFocus="true"                  
        android:singleLine="true"
        android:layout="@layout/preference_screen_layout"
        android:textSize="@dimen/text_size"
        android:title="@string/pref_title_internet_addr"/>
    <...>
</PreferenceScreen>
但似乎无法让任何一个起作用

希望我的应用程序的截图能帮助我理解我想要实现的目标

这将显示单击EditTextPreference后弹出的对话框。“取消”和“确定”按钮上的文本大小是我试图更改的。见红色箭头。此对话框中的其他文本由EditTextPreference中的属性控制。

任何帮助都将不胜感激


我发现了要使用的正确样式参数:

应用主题定义:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:editTextPreferenceStyle">@style/DialogPreferenceStyle</item>
</style>

<style name="DialogPreferenceStyle" parent="Preference.DialogPreference.EditTextPreference">
    <item name="android:positiveButtonText">TEST TEXT</item>
</style>

@样式/对话框首选样式
测试文本
截图:

从屏幕截图中可以看到,我可以更改按钮的文本,但无法更改文本大小

这样行吗

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
    android:title="First Category"
    android:textSize="20px"
    android:layout="@layout/mylayout">   

观察结果:
通过查看
EditTextPreference
的超类的源代码,我们将看到按钮文本大小没有属性。这意味着正常情况下,无法将按钮文本大小从
style
xml
更改


解决方案:
因此,进行此类定制的唯一方法是扩展
EditTextPreference
。我编写了一个从
EditTextPreference
继承的自定义类,它覆盖
showDialog()
对其进行更改。请看这个:

MyEditTextPreference.java

package com.aminography;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.os.Build;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.Button;

import com.aminography.R;

public class MyEditTextPreference extends EditTextPreference {

    private float buttonTextSize = 0;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context, attrs, defStyleAttr, defStyleRes);
    }

    public MyEditTextPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr, 0);
    }

    public MyEditTextPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0, 0);
    }

    public MyEditTextPreference(Context context) {
        super(context);
    }

    private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyEditTextPreference, defStyleAttr, defStyleRes);
        buttonTextSize = typedArray.getDimension(R.styleable.MyEditTextPreference_buttonTextSize, 0);
        typedArray.recycle();
    }

    @Override
    protected void showDialog(Bundle state) {
        super.showDialog(state);

        AlertDialog dialog = (AlertDialog) getDialog();
        if (dialog != null && buttonTextSize > 0) {
            Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            positiveButton.setTextSize(TypedValue.COMPLEX_UNIT_PX, buttonTextSize);
        }
    }

}
xml/preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.aminography.MyEditTextPreference
        android:key="@string/pref_key_username"
        android:summary="Please enter your username:"
        android:title="Your Name"
        app:buttonTextSize="30sp" />

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyEditTextPreference">
        <attr name="buttonTextSize" format="dimension"/>
    </declare-styleable>
</resources>

值/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.aminography.MyEditTextPreference
        android:key="@string/pref_key_username"
        android:summary="Please enter your username:"
        android:title="Your Name"
        app:buttonTextSize="30sp" />

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyEditTextPreference">
        <attr name="buttonTextSize" format="dimension"/>
    </declare-styleable>
</resources>


您也可以在
showDialog()
中将文本大小设置为
按钮\u负值。

我尝试过这个方法,但它只影响首选项屏幕,而不影响选择EditTextPreference后弹出的对话框。@tzg:感谢您分配奖金。问题还没有解决吗?
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <com.aminography.MyEditTextPreference
        android:key="@string/pref_key_username"
        android:summary="Please enter your username:"
        android:title="Your Name"
        app:buttonTextSize="30sp" />

</PreferenceScreen>
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyEditTextPreference">
        <attr name="buttonTextSize" format="dimension"/>
    </declare-styleable>
</resources>