简体   繁体   中英

Change text size on the positveButtonText in the EditTextPreference dialog

I cannot figure out how to change the text size on the buttons in the EditTextPreference dialog popup box. I would like all my text throughout my app to be streamlined at 24 sp. This is happening everywhere except this dialog box from the PreferenceScreen. As you can see in the ListPreference I figured out how to hide the positiveButtonText and negativeButtonText.

I would like to do this in the xml, if possible. I also would like to avoid having to create a custom dialog layout. In the EditTextPreference tag I can edit the title and layout in the dialog, I just can't figure out how to "touch" the default buttons.

Here is my 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>

Here is the layout:

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

Would like to do something simple like styles:

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

or utilize dialogLayout inside the EditTextPreference:

android:dialogLayout="@layout/dialogLayout"

But can't seem to get any of those working.

Hopefully screenshots of my app will help with understanding what I'm trying to accomplish.

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. The other text in this dialog are controlled by the attributes in the EditTextPreference. 这显示单击EditTextPreference后弹出的对话框。我正在尝试更改“取消”和“确定”按钮上的文本大小。见红色箭头。

Any help would be greatly appreciated.


I discovered the correct style parameters to use:

AppTheme definitions:

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

Screenshot:

在此处输入图片说明

As you can see from the screenshot, I'm able to change the text of the button just can't change the text size.

Observations:

By looking at source code of DialogPreference which is EditTextPreference 's super class, we will see that there is no attributes for button text size. It means that it is not possible to change the buttons text size from style and xml normally.

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(); } 

Solution:

Therefore, the only way to do such customization is extending the EditTextPreference . I wrote a custom class inheriting from EditTextPreference which overrides showDialog() to make the change on it. Look at this please:

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>

.

Visual Result:

在此处输入图片说明

You can set the text size to BUTTON_NEGATIVE in showDialog() , too.

Would this work?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM