栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Java 是否可以为整个应用程序设置自定义字体?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 是否可以为整个应用程序设置自定义字体?

是的,反思。这有效(基于此答案):

(注意:由于缺乏对自定义字体的支持,因此这是一种解决方法,因此,如果你要更改此情况,请在此处加注星号以投票赞成android问题)。注意:请勿在该问题上留下“我也”评论,当你这样做时,所有盯着它的人都会收到一封电子邮件。因此,请对其“加星标”。

import java.lang.reflect.Field;import android.content.Context;import android.graphics.Typeface;public final class FontsOverride {    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();        }    }}

然后,你需要重载一些默认字体,例如在应用程序类中:

public final class Application extends android.app.Application {    @Override    public void onCreate() {        super.onCreate();        FontsOverride.setDefaultFont(this, "DEFAULT", "MyFontAsset.ttf");        FontsOverride.setDefaultFont(this, "MONOSPACE", "MyFontAsset2.ttf");        FontsOverride.setDefaultFont(this, "SERIF", "MyFontAsset3.ttf");        FontsOverride.setDefaultFont(this, "SANS_SERIF", "MyFontAsset4.ttf");    }}

或者当然,如果你使用的是相同的字体文件,则可以对此进行改进以仅将其加载一次。

但是,我倾向于只覆盖一个,比如说”MONOSPACE”,然后设置一种样式来强制该字体应用程序范围广泛:

<resources>    <style name="AppbaseTheme" parent="android:Theme.Light">    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppbaseTheme">        <item name="android:typeface">monospace</item>    </style></resources>

API 21 Android 5.0

我已经对评论中的报告进行了调查,认为该报告无效,并且似乎与主题不兼容

android:Theme.Material.Light

如果该主题对你不重要,请使用较旧的主题,例如:

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/392675.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号