1. ホーム
  2. Android development

Android studioでTextViewのフォントを変更する2つの方法(例:模造歌、公式スクリプト)。

2022-02-18 04:49:05
<パス

Androidのデフォルトフォントは3種類あり、TextViewにandroid:typeface="sans"を追加することで変更可能で、sansはそのうちの一つ、他の2つは" monospace" と" serif" で、3通りの使い方をしたくない場合は、外部フォントを使うことも可能です。


外部フォントを利用する方法はたくさんありますが、ここでは以下のような効果を持つ2つの方法を簡単に紹介します。

最初の方法 TextViewのフォントをコードで定義する。
ステップス
1. まず、図のようにsrc/mainファイルの下にassetsフォルダを作成し、その下に仕様のためにfontsフォルダを作成し、その下に弊社のフォントファイル(*.ttf)を配置することにします。

2. TextView コントロールをドラッグして、レイアウトファイルで準備します。

3. コード内でインポートしたフォントを見つけ、TextViewで使用します。


エフェクト


2つ目の方法:TextViewを継承したクラスを作成し、すべてをこのクラスに入れ、フォントを変更したい場所でこのクラスを使用します。
ステップス
1、新しいクラスを作成し、このクラスがTextViewを継承するようにします。

/**
 * 1.version:
 * 2.date:2017/1/13 15:26
 * 3.update:2017/1/13.
 * 4.autour:Zhang Yujie
 */

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util;
import android.widget;

TextView; /**
 TextView; /** * Created by Administrator on 2015/10/27.
 */TextView; /** * Created by Administrator on 2015/10/27.
public class FontTextView extends TextView {
    /* The file name of the font data in the assets directory */
    private String mFontPath = null;

    public FontTextView(Context context) {
        super(context);
        init(context, null, 0);
    }
    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }
    public FontTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs); init(context, attrs, 0); }
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public String getFontPath() {
        return mFontPath;
    }

    /**
     * 

Set font file fontPath

* @param fontPath The file name of the font data in the assets directory */ public void setFontPath(String fontPath) { mFontPath = fontPath; if (!TextUtils.isEmpty(mFontPath)) { FontUtils.getInstance().replaceFontFromAsset(this, mFontPath); } } private void init(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FontTextView, defStyleAttr, 0); mFontPath = typedArray.getString(R.styleable.FontTextView_font_path); typedArray.recycle(); if (!TextUtils.isEmpty(mFontPath)) { FontUtils.getInstance().replaceFontFromAsset(this, mFontPath); } } }

2. フォントを選択するクラスを作成する

/**
 * 1.version:
 * 2.date:2017/1/13 15:27
 * 3.update:2017/1/13.
 * 4.autour:ZhangYuJie
 */

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.lang.ref.SoftReference;
import java.lang.ref;
import java.util.HashMap;
import java.util.Map;

public class FontUtils {
    private static final String TAG = FontUtils.class.getSimpleName();
    private Map<String, SoftReference<Typeface>> mCache = new HashMap<>();
    private static FontUtils sSingleton = null;

    public static Typeface DEFAULT = Typeface;

    // disable instantiate
    private FontUtils() {
    }

    public static FontUtils getInstance() {
        // double check
        if (sSingleton == null) {
            synchronized (FontUtils.class) {
                if (sSingleton == null) {
                    sSingleton = new FontUtils();
                }
            }
        }
        return sSingleton;
    }

    public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath) {
        replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath));
    }

    public void replaceFontFromAsset(@NonNull View root, @NonNull String fontPath, int style) {
        replaceFont(root, createTypefaceFromAsset(root.getContext(), fontPath), style);
    }

    public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath) {
        replaceFont(root, createTypefaceFromFile(fontPath));
    }

    public void replaceFontFromFile(@NonNull View root, @NonNull String fontPath, int style) {
        replaceFont(root, createTypefaceFromFile(fontPath), style);
    }

    private void replaceFont(@NonNull View root, @NonNull Typeface typeface) {
        if (root == null || typeface == null) {
            return;
        }

        if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
            TextView textView = (TextView) root;
            // Extract previous style of TextView
            int style = Typeface.NORMAL;
            if (textView.getTypeface() ! = null) {
                style = textView.getTypeface().getStyle();
            }
            textView.setTypeface(typeface, style);
        } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
            ViewGroup viewGroup = (ViewGroup) root;
            for (int i = 0; i < viewGroup.getChildCount(); ++i) {
                replaceFont(viewGroup.getChildAt(i), typeface);
            }
        } // else return
    }

    private void replaceFont(@NonNull View root, @NonNull Typeface typeface, int style) {
        if (root == null || typeface == null) {
            return;
        }
        if (style < 0 || style > 3) {
            style = Typeface.NORMAL;
        }

        if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's font
            TextView textView = (TextView) root;
            textView.setTypeface(typeface, style);
        } else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child views
            ViewGroup viewGroup = (ViewGroup) root;
            for (int i = 0; i < viewGroup.getChildCount(); ++i) {
                replaceFont(viewGroup.getChildAt(i), typeface, style);
            }
        } // else return
    }

    private Typeface createTypefaceFromAsset(Context context, String fontPath) {
        SoftReference<Ty

3. レイアウトファイルの内部でクラスの完全なクラス名を直接使用し、ライブビューでその効果を確認できるようにします。

レンダリング

プロジェクトのアドレスです。
http://download.csdn.net/detail/lanrenxiaowen/9737027