android - 在 EditTextPreference 对话框中更改 positveButtonText 上的文本大小

标签 android dialog preference preferencescreen edittextpreference

我不知道如何更改 EditTextPreference 对话框弹出框中按钮上的文本大小。我希望我的应用程序中的所有文本都以 24 sp. 的速度进行精简。除了 PreferenceScreen 中的这个对话框外,这种情况无处不在。正如您在 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>

这是布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@android:id/title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/large_margin"
        android:paddingBottom="@dimen/margin"
        android:textSize="@dimen/text_size"/>

    <TextView android:id="@android:id/summary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="@dimen/large_margin"
        android:textSize="@dimen/text_size"/>

</LinearLayout>

想做一些像样式一样简单的事情:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
    <item name="android:textSize">24sp</item>
</style>

或在 EditTextPreference 中使用 dialogLayout:

android:dialogLayout="@layout/dialogLayout"

但似乎无法让其中任何一个工作。

希望我的应用程序的屏幕截图有助于理解我要完成的任务。

这显示了单击 EditTextPreference 后弹出的对话框。取消和确定按钮上的文字大小是我要更改的内容。见红色箭头。此对话框中的其他文本由 EditTextPreference 中的属性控制。 This shows the dialog box that pops up after the EditTextPreference is clicked. The text size on the cancel and ok buttons are what I'm trying to change. See red arrows.

如有任何帮助,我们将不胜感激。


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

AppTheme 定义:

<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>

截图:

enter image description here

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

最佳答案

观察:

通过查看 DialogPreference 的源代码这是 EditTextPreference 的父类(super class),我们将看到没有按钮文本大小的属性。这意味着 不可能stylexml 正常更改按钮文本大小。

DialogPreference.java

public DialogPreference(
        Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    final TypedArray a = context.obtainStyledAttributes(attrs,
            com.android.internal.R.styleable.DialogPreference, defStyleAttr, defStyleRes);
    mDialogTitle = a.getString(com.android.internal.R.styleable.DialogPreference_dialogTitle);
    if (mDialogTitle == null) {
        // Fallback on the regular title of the preference
        // (the one that is seen in the list)
        mDialogTitle = getTitle();
    }
    mDialogMessage = a.getString(com.android.internal.R.styleable.DialogPreference_dialogMessage);
    mDialogIcon = a.getDrawable(com.android.internal.R.styleable.DialogPreference_dialogIcon);
    mPositiveButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_positiveButtonText);
    mNegativeButtonText = a.getString(com.android.internal.R.styleable.DialogPreference_negativeButtonText);
    mDialogLayoutResId = a.getResourceId(com.android.internal.R.styleable.DialogPreference_dialogLayout,
            mDialogLayoutResId);
    a.recycle();
}

解决方案:

因此,进行此类自定义的唯一方法是扩展 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>

values/attrs.xml

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

.

视觉效果:

enter image description here

您也可以在 showDialog() 中将文本大小设置为 BUTTON_NEGATIVE

关于android - 在 EditTextPreference 对话框中更改 positveButtonText 上的文本大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55540185/

相关文章:

android - 如何验证蓝牙耳机是否在安卓上连接?

facebook - 是否可以知道用户使用 Facebook 发送对话框向哪些 friend 发送了消息?

android - 显示对话框后如何关闭应用程序?

Android:如何在Preference中调整Margin/Padding?

android - 如何制作包含标题和 2 个按钮的操作栏?

android - Webview 在启动时崩溃 android 应用程序

android - Google Play 服务已过时。需要 11910000 但找到 11577470

jquery - 相对于元素和窗口的定位

android - 检查设备是否锁定在纵向模式

visual-studio-code - 如何更改 Visual Studio Code 中的集成终端