更改Android EditTextPreference的fontFamily

更改Android EditTextPreference的fontFamily,android,android-studio,edittextpreference,Android,Android Studio,Edittextpreference,我正在尝试更改EditTextPreference fontFamily,以便使用自定义字体资源显示所有可见文本(包括首选项标题和对话框标题)。有什么建议吗?我尝试在styles.xml中建立fontFamily,并创建自定义EditTextPreference。谢谢 为了便于参考,我附加了当前视图(请注意工具栏标题和首选项小部件之间的字体差异): 此代码可以为您提供: 在活动中,请调用此: ViewGroup vg = (ViewGroup) getWindow().getDecorView(

我正在尝试更改EditTextPreference fontFamily,以便使用自定义字体资源显示所有可见文本(包括首选项标题和对话框标题)。有什么建议吗?我尝试在styles.xml中建立fontFamily,并创建自定义EditTextPreference。谢谢

为了便于参考,我附加了当前视图(请注意工具栏标题和首选项小部件之间的字体差异):

此代码可以为您提供:

在活动中,请调用此:

ViewGroup vg = (ViewGroup) getWindow().getDecorView();
CustomTypeface.setTypeFace(Typeface.createFromAsset(context.getAssets(), "helvetica.ttf"), vg);
并同意此方法:

import android.graphics.Paint;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.dlbz.pos.activity.menu.R;

public class CustomTypeface {
    public static void setTypeFace(Typeface typeFace, ViewGroup parent) {
        for (int i = 0; i < parent.getChildCount(); i++) {
            View v = parent.getChildAt(i);
            if (v instanceof ViewGroup) {
                setTypeFace(typeFace, (ViewGroup) v);
            } else if (v instanceof TextView) {
                TextView tv = (TextView) v;
                tv.setTypeface(typeFace);
                tv.setPaintFlags(tv.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
            } else if (v instanceof Button) {
                Button btn = (Button) v;
                btn.setTypeface(typeFace);
                btn.setPaintFlags(btn.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
            } else if (v instanceof EditText) {
                EditText et = (EditText) v;
                et.setTypeface(typeFace);
                et.setPaintFlags(et.getPaintFlags() | Paint.SUBPIXEL_TEXT_FLAG);
            }
        }
    }
}
导入android.graphics.Paint;
导入android.graphics.Typeface;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入com.dlbz.pos.activity.menu.R;
公共类自定义字体{
公共静态void setTypeFace(字体、视图组父级){
对于(int i=0;i

这个方法将所有字体改为viewgroup中的view(查看)

谢天谢地,我终于找到了问题的答案。解决方案包括创建自定义布局并在my preferences.xml文件中引用该布局。下面是它的故障原因:

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
android:fragment="com.app_name.fragment">
    <EditTextPreference
        android:id="@+id/key_Edit"
        android:clickable="true"
        android:inputType="textCapCharacters|textMultiLine"
        android:enabled="true"
        android:gravity="top"
        android:key="pref_phrase"
        android:singleLine="true"
        android:textAlignment="center"
        android:title="@string/preference_key"
        android:defaultValue=" "
        android:selectAllOnFocus="true"
        android:fontFamily="@font/my_font"
        android:textColor="@color/colorPrimary"
        android:layout="@layout/preference_layout"/>
    <EditTextPreference
        android:id="@+id/password_Edit"
        android:gravity="top"
        android:key="pref_password"
        android:selectAllOnFocus="true"
        android:singleLine="true"
        android:textAlignment="center"
        android:title="@string/preference_security"
        android:defaultValue=" "
        android:inputType="number"
        android:maxLength="5"
        android:fontFamily="@font/my_font"
        android:textColor="@color/colorPrimary"
        android:layout="@layout/preference_layout"/>
</PreferenceScreen>

preference_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="?attr/listPreferredItemHeight"
    android:paddingEnd="?attr/listPreferredItemPaddingRight"
    android:paddingLeft="?attr/listPreferredItemPaddingLeft"
    android:paddingRight="?attr/listPreferredItemPaddingRight"
    android:paddingStart="?attr/listPreferredItemPaddingLeft">
    <android.support.constraint.ConstraintLayout
        android:id="@+id/layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">
        <TextView
            android:id="@android:id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/my_font"
            android:textAppearance="@style/TextAppearance.AppCompat.Medium"
            android:textColor="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@id/layout" />
        <TextView
            android:id="@android:id/summary"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="@font/my_font"
            android:textAppearance="@style/TextAppearance.AppCompat.Small"
            android:textColor="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@android:id/title"
            android:paddingTop="4dp"/>
    </android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>

如果还想修改使用这些首选项展开的对话框的字体,则必须创建自定义EditTextPreference并使用关联的对话框生成器。希望它能帮助你

编辑

如果您习惯于使用styles.xml,EditTextPreferences最终将在创建和显示AlertDialogs对话框时继承AlertDialogs。因此,可以为AlertDialogs创建自定义主题,在其中设置fontFamily。因此:

<style name="AlertDialogTheme" parent="Theme.AppCompat.Light.Dialog.Alert">
        <item name="android:fontFamily">@font/my_font</item>
        <item 
name="android:textAppearance">@style/TextAppearance.AppCompat.Medium</item>
        <item name="android:textStyle">bold</item>
        <item name="android:windowNoTitle">true</item>
    </style>

@字体/我的
@style/TextAppearance.AppCompat.Medium
大胆的
真的

最后,使用自定义的AlertDialogTheme覆盖应用程序主题中的标准AlertDialogTheme。

嗨,Kenji,感谢您的贡献。我运行了您的代码,不幸的是,它没有更改EditTextPreferences的fontFamily。张贴图片中显示的状态未更改。