• Android 导入外部字体的完美解决方案


    第一步:在main文件夹下添加assets文件,下面再建fonts文件夹 然后引入外部字体。

    第二步:建一个字体工具类。

     1 package com.xxxx.xxxx.fonts;
     2 
     3 import android.content.Context;
     4 import android.graphics.Typeface;
     5 
     6 import java.lang.reflect.Field;
     7 
     8 /**
     9  * 覆盖字体工具类
    10  * Created by leict on 2017/2/28.
    11  */
    12 
    13 public final class FontsOverride {
    14     /**
    15      * 设置默认字体
    16      *
    17      * @param context
    18      * @param staticTypefaceFieldName
    19      * @param fontAssetName
    20      */
    21     public static void setDefaultFont(Context context, String staticTypefaceFieldName, String fontAssetName) {
    22         final Typeface regular = Typeface.createFromAsset(context.getAssets(), fontAssetName);
    23         replaceFont(staticTypefaceFieldName, regular);
    24     }
    25 
    26     /**
    27      * 替换字体
    28      *
    29      * @param staticTypefaceFieldName
    30      * @param newTypeface
    31      */
    32     protected static void replaceFont(String staticTypefaceFieldName, final Typeface newTypeface) {
    33         try {
    34             final Field staticField = Typeface.class.getDeclaredField(staticTypefaceFieldName);
    35             staticField.setAccessible(true);
    36             staticField.set(null, newTypeface);
    37         } catch (NoSuchFieldException | IllegalAccessException e) {
    38             e.printStackTrace();
    39         }
    40     }
    41 }

    第三步:在全局中初始化。

     1 package com.xxxx.xxxxx.application;
     2 
     3 import android.app.Application;
     4 
     5 import com.xxxx.travel.fonts.FontsOverride;
     6 
     7 /**
     8  * 全局变量
     9  * Created by leict on 2017/2/27.
    10  */
    11 
    12 public class BaseApplication extends Application {
    13     @Override
    14     public void onCreate() {
    15         super.onCreate();
    16         FontsOverride.setDefaultFont(this, "MONOSPACE", "fonts/simkai.ttf");
    17         FontsOverride.setDefaultFont(this, "SANS", "fonts/simhei.ttf");
    18         FontsOverride.setDefaultFont(this, "SERIF", "fonts/simfang.ttf");
    19     }
    20 }

    第四步:xml代码展示。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
    
        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="世界你好"
                    android:textSize="24sp"
                    android:typeface="monospace" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="世界你好"
                    android:textSize="24sp"
                    android:typeface="sans" />
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="世界你好"
                    android:textSize="24sp"
                    android:typeface="serif" />
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="世界你好!"
                    android:textSize="24sp"
                    android:typeface="serif" />
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="世界你好!"
                    android:textSize="24sp"
                    android:typeface="sans" />
    
                <EditText
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="世界你好!"
                    android:textSize="24sp"
                    android:typeface="monospace" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="提交"
                    android:textSize="24sp"
                    android:typeface="serif" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="提交"
                    android:textSize="24sp"
                    android:typeface="sans" />
    
                <Button
                    android:layout_width="match_parent"
                    android:layout_height="60dp"
                    android:text="提交"
                    android:textSize="24sp"
                    android:typeface="monospace" />
            </LinearLayout>
        </ScrollView>
    </LinearLayout>

    效果图:

  • 相关阅读:
    前端总结挺全面的
    cmd与bat脚本的使用
    Spring控制反转(IoC)的理解
    C# 中迭代器
    URL 分页并排序
    结合Flash上传文件时显示进度条
    C# 语言特性
    where T:new()
    图片缩放特效
    C# 隐藏窗体 ALT+TAb不可见
  • 原文地址:https://www.cnblogs.com/xiaoyao095/p/6478194.html
Copyright © 2020-2023  润新知