Android 应用中无法更改默认字体

5

我正在尝试更改我的应用程序中的默认字体,但是没有成功。这是我采取的步骤:

1)创建了类TypefaceUtil.java

import android.content.Context;
import android.graphics.Typeface;
import android.util.Log;

import java.lang.reflect.Field;

public class TypefaceUtil {

public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) {
    try {
        final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

        final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
        defaultFontTypefaceField.setAccessible(true);
        defaultFontTypefaceField.set(null, customFontTypeface);
    } catch (Exception e) {
        Log.e("CustomFontException", "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
    }
  }
}

2) 在扩展Application的类中:

public void onCreate() {

     super.onCreate();
     TypefaceUtil.overrideFont(getApplicationContext(), "MONOSPACE", "fonts/varelaround_regular.ttf");
 }

3) 在 styles.xml

 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
     <item name="android:typeface">monospace</item>
 </style>

仍然无法工作。我漏掉了什么吗?

奇怪,我使用了相同的代码,而且对我来说工作得很好。唯一的区别是我使用了 serif 而不是 monospace - K Neeraj Lal
3个回答

2

我曾经遇到过这个问题。我不太确定为什么它有效,但你可以尝试以下操作: 不要使用"monospace",而是尝试以下每一个字体: DEFAULT(默认),SANS_SERIF(无衬线字体),SERIF(衬线字体)。它可能对于所有的文本视图都不能正常工作,例如ListView或recyclerView中的文本视图(很奇怪,对吧?)。但在这些情况下,我从适配器中以程序化方式设置了字体。 对于无法解释的原因感到抱歉。


1
为此,我强烈推荐您使用 Calligraphyhttps://github.com/chrisjenx/Calligraphy,这是一个非常棒的库,可以轻松更改默认字体,并具有许多其他有用的功能。
设置所需的一切都应在自述文件中。

0
为什么不定义自己的自定义TextView并分配任何字体给它呢?
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.EditText;

import com.exmple.util.Utility;

public class CustomFontEditText extends EditText {

    private Context mContext;

    private String ttfName;

    String TAG = getClass().getName();

    public CustomFontEditText(Context context) {
        super(context);
        this.mContext = context;
    }

    public CustomFontEditText(Context context, AttributeSet attrs) {
        super(context, attrs);

        this.mContext = context;

        // Typeface.createFromAsset doesn't work in the layout editor.
        // Skipping...
        if (isInEditMode()) {
            return;
        }

        for (int i = 0; i < attrs.getAttributeCount(); i++) {
            this.ttfName = attrs.getAttributeValue(Utility.ATTRIBUTE_SCHEMA,
                    Utility.ATTRIBUTE_TTF_KEY);

            if (null != ttfName)
                init();
        }

    }

    public CustomFontEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
    }

    private void init() {
        Typeface font = Utility.getFonts(mContext, ttfName);
        if (null != font)
            setTypeface(font);
    }

    @Override
    public void setTypeface(Typeface tf) {

        super.setTypeface(tf);
    }

}

在您的实用程序类中,从资产加载并存储字体到哈希映射中,以便稍后可以重复使用字体。想象一下每次加载新视图时都要加载字体... OOM..!!!
private static Map<String, Typeface> TYPEFACE = new HashMap<String, Typeface>();
public static Typeface getFonts(Context context, String fontName) {
    Typeface typeface = TYPEFACE.get(fontName);
    if (typeface == null) {
        typeface = Typeface.createFromAsset(context.getAssets(), "fonts/"+fontName);
        TYPEFACE.put(fontName, typeface);
    }
    return typeface;
}

在XML中添加ttf属性以设置字体

public static final String ATTRIBUTE_TTF_KEY = "ttf_name";

public static final String ATTRIBUTE_SCHEMA = "http://schemas.android.com/apk/lib/com.exmaple.ui.customfont";

现在在您的布局文件中使用

 <com.example.ui.customviews.CustomFontEditText
            xmlns:font="http://schemas.android.com/apk/lib/com.exmaple.ui.customfont"
            android:id="@+id/edt_pay_input"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginRight="20dp"
            android:background="@null"
            android:cursorVisible="false"
            android:drawableBottom="@drawable/dashed_white"
            android:includeFontPadding="false"
            android:inputType="numberDecimal"
            android:maxLength="4"
            android:minWidth="20dp"
            android:singleLine="true"
            android:textColor="@color/white"
            android:textSize="30sp"

            font:ttf_name="FedraSansStd-Light.otf" >

            <requestFocus />
        </com.example.ui.customviews.CustomFontEditText>

你可以对Button、TextView等控件做同样的操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接