conlan
论坛版主
论坛版主
阅读:5545回复:2

安卓修改字体

楼主#
更多 发布于:2019-07-10 14:20
大神们,安卓怎么修改字体?

最新喜欢:

zhaoyf13zhaoyf... VastStarGamesVastSt...
沙发#
发布于:2019-07-10 23:00
之前我给你发的工程就是用的自定义字体,你看下我给你发的代码。
If you have nothing to lose, then you can do anything.
板凳#
发布于:2019-07-11 09:18
APP内使用自带的字体(所有的控件都一次性换过来了):


fonts/C010012D_1.TTF字体文件放在工程的asserts目录下即可。
以下函数在APP创建的时候调用,例如可以在Application的继承类中调用(该类一个APP只能定义一个)

private void init_app_typeface() {
            Typeface newTypeface = Typeface.createFromAsset(this.getAssets(), "fonts/C010012D_1.TTF");
            try {
                //android 5.0及以上我们反射修改Typeface.sSystemFontMap变量
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Map<String, Typeface> newMap = new HashMap<>();
                    newMap.put("SERIF", newTypeface);
                    final Field staticField = Typeface.class.getDeclaredField("sSystemFontMap");
                    staticField.setAccessible(true);
                    staticField.set(null, newMap);
                } else {
                    final Field staticField = Typeface.class.getDeclaredField("SERIF");
                    staticField.setAccessible(true);
                    staticField.set(null, newTypeface);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
}
If you have nothing to lose, then you can do anything.
游客

返回顶部