Android 应用的默认字体

Default font for an Android app

这是对这个问题的跟进:

How to set default font family for entire Android app

我得到了一些自定义字体,并将其添加到 assets/fonts 文件夹中。现在我想使用该库中的一种字体作为整个应用程序的默认字体。

上面链接问题的可接受答案建议使用我想要的样式,但属性 "android:fontFamily" 仅在 API 16 时添加。有没有办法也为较旧的 API 使用样式?

如果您想要 android 应用程序的默认字体,您可以使用:

Calligraphy Library

并且您需要从您的应用程序初始化它 class 一旦您这样做,自定义字体将适用于该应用程序的所有活动。

只需按照 lib wiki 进行设置即可。

在您的资产资源中添加字体 ttf 文件

试试下面的 class 自定义文本视图并在您的 xml 文件中添加自定义 class

public class CustomTextView extends TextView {


    public CustomTextView(Context context) {
        super(context);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setTypeface(getTypeFace(context));
    }

    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.setTypeface(getTypeFace(context));
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.setTypeface(getTypeFace(context));
    }


    public Typeface getTypeFace(Context context) {
        Typeface font = Typeface.createFromAsset(context.getAssets(), "your_filename.ttf");
        return font;
    }
}

在 xml 文件中:

<package_name.CustomTextView
        android:id="@+id/tv_downloading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/icon_close"
        android:textSize="100dp"
        android:textStyle="bold"
        android:layout_centerInParent="true"
        android:background="@color/col_white"
        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible" />

我尝试搜索解决方案,发现您无法将自定义字体添加为小于 16 的较低版本的默认字体。
它适用于内置字体。 这是一个可能的 solution

对于"android:fontFamily"
- 仅接受您的 OS version LINK.
提供的默认字体 - 在 Android "O" 之后,您可以将它与您的 resources 文件夹一起使用 LINK

所以,

如果您有现有项目,在您的应用程序中设置字体的最佳和更快的方法是Calligraphy Library @Oussaki 的建议。

<TextView
    android:text="@string/hello_world"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    fontPath="fonts/Roboto-Bold.ttf"/>

它只需要 fontPath="fonts/Roboto-Bold.ttf" 整合 之后

也可用于 Eclipse .Aar file

编辑

不要忘记将此行放在每个根布局中。 LINK

tools:ignore="MissingPrefix"

快乐编码:)

我最终做的是首先添加这个 class:

public class TypefaceUtil 
{

/**
 * Using reflection to override default typeface
 * NOTICE: DO NOT FORGET TO SET TYPEFACE FOR APP THEME AS DEFAULT TYPEFACE WHICH WILL BE OVERRIDDEN
 *
 * @param context                    to work with assets
 * @param defaultFontNameToOverride  for example "monospace"
 * @param customFontFileNameInAssets file name of the font from assets
 */
public static void overrideFont(Context context, String defaultFontNameToOverride, String customFontFileNameInAssets) 
{
    final Typeface customFontTypeface = Typeface.createFromAsset(context.getAssets(), customFontFileNameInAssets);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) 
    {
        Map<String, Typeface> newMap = new HashMap<String, Typeface>();
        newMap.put("serif", customFontTypeface);
        try 
        {
            final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
            staticField.setAccessible(true);
            staticField.set(null, newMap);
        } 
        catch (NoSuchFieldException e) 
        {
            e.printStackTrace();
        } 
        catch (IllegalAccessException e) 
        {
            e.printStackTrace();
        }
    } 
    else 
    {
        try 
        {
            final Field defaultFontTypefaceField = Typeface.class.getDeclaredField(defaultFontNameToOverride);
            defaultFontTypefaceField.setAccessible(true);
            defaultFontTypefaceField.set(null, customFontTypeface);
        } catch (Exception e) 
        {
            Log.e(TypefaceUtil.class.getSimpleName(), "Can not set custom font " + customFontFileNameInAssets + " instead of " + defaultFontNameToOverride);
        }
    }
}

}

然后我在 Application class:

中添加了这一行
@Override
public void onCreate()
{
    super.onCreate();
    TypefaceUtil.overrideFont(getApplicationContext(), "serif", "fonts/Ubuntu-Regular.ttf");
}

保存字体本身的 fonts 目录位于 assets 目录中,该目录位于 src\main (src\main\assets\fonts)