【问题标题】:Change Android System Font programmatically以编程方式更改 Android 系统字体
【发布时间】:2014-09-18 18:34:29
【问题描述】:

我正在开发一个 android 应用程序,但对如何更改 android 上的默认字体一无所知。我不想根系统来更改字体。必须在整个系统中更改字体。

【问题讨论】:

标签: android fonts


【解决方案1】:

setTypeface() 的唯一问题是,如果你想在应用范围内使用它,你必须在每个视图中都使用它。这要么意味着大量的冗余代码,要么意味着编写自定义视图类来设置字体膨胀。或者你可以在帮助类中使用这两个漂亮的小方法通过反射设置你的应用程序字体:

public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
    final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
    replaceFont(staticTypefaceFieldName, regular);
}

protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
    try {
        final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
        staticField.setAccessible(true);
        staticField.set(null, newTypeface);
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }
}

然后设置字体做类似MyHelperClass.setDefaultFont(this, "DEFAULT", "fonts/FONT_NAME.ttf");

这会将您的应用的默认字体设置为您的 FONT_NAME.ttf 字体文件。

【讨论】:

    【解决方案2】:

    你可以把你的自定义字体放在assets/fonts

    它需要以 .ttf 扩展名结尾 而且比

    Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
            "fonts/yourFont.ttf");
        setTypeface(tf);
    

    【讨论】:

      【解决方案3】:

      创建资产/字体

      示例代码:

        number_text = (TextView)findViewById(R.id.hour_progress_number);
        number_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
      
        minute_text = (TextView)findViewById(R.id.minute_progress_number);
        minute_text.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/roboto-thin.ttf"));
      

      【讨论】:

      • 答案是你不能这样做。应用程序无权访问系统的该部分(如您所说,没有 root 访问权限)。
      • 你确定吗?
      猜你喜欢
      • 1970-01-01
      • 2013-08-21
      • 2010-10-13
      • 2016-10-26
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 1970-01-01
      相关资源
      最近更新 更多