简体   繁体   English

为Android EditTextPreference更改fontFamily

[英]Change fontFamily for Android EditTextPreference

I am trying to change the EditTextPreference fontFamily so that all visible text (including the Preference title and Dialog title) are displayed with a custom font resource. 我试图更改EditTextPreference fontFamily,以便所有可见的文本(包括Preference标题和Dialog标题)都使用自定义字体资源显示。 Any suggestions? 有什么建议么? I have tried establishing a fontFamily in the styles.xml as well as creating a custom EditTextPreference. 我尝试在styles.xml中建立fontFamily以及创建自定义EditTextPreference。 Thanks! 谢谢!

For reference, I have attached the current view (Note the difference in font between the toolbar title and preference widgets): Settings page 作为参考,我附加了当前视图(请注意工具栏标题和首选项小部件之间的字体差异): 设置页面

this code can serve you: 该代码可以为您服务:

In activity call this: 在活动中称为:

ViewGroup vg = (ViewGroup) getWindow().getDecorView();
CustomTypeface.setTypeFace(Typeface.createFromAsset(context.getAssets(), "helvetica.ttf"), vg);

And agree this method: 并同意此方法:

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

This method change all typeface to view in viewgroup 此方法将所有字体更改为在视图组中查看

I thankfully managed to find an answer to my question. 幸运的是,我设法找到了我的问题的答案。 The solution involved creating a custom layout and referencing that in my preferences.xml file. 解决方案涉及创建自定义布局,并在我的preferences.xml文件中引用它。 Here's how it broke down: 它是如何分解的:

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

If you intend to also modify the font of the dialog that inflates with these preferences, you will have to create a custom EditTextPreference and work with the associated dialog builder. 如果您还打算修改使用这些首选项膨胀的对话框的字体,则必须创建一个自定义EditTextPreference并使用关联的对话框构建器。 Hope it helps you! 希望对您有帮助!

EDIT 编辑

If you are comfortable using styles.xml, EditTextPreferences ultimately inherit from AlertDialogs in creating and displaying their dialogs. 如果您习惯使用styles.xml,则EditTextPreferences最终会在创建和显示其对话框时从AlertDialogs继承。 Thus, you can create a custom theme for AlertDialogs in which you set the fontFamily. 因此,您可以为AlertDialogs创建自定义主题,并在其中设置fontFamily。 Thus: 从而:

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

Lastly, override the standard AlertDialogTheme in your application's theme using that custom AlertDialogTheme. 最后,使用该自定义AlertDialogTheme覆盖应用程序主题中的标准AlertDialogTheme。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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