Android—自定义TextView字体!

本文介绍了两种在Android中自定义TextView字体的方法:一种是直接在代码中设置,另一种是创建自定义字体TextView基类,并在XML布局中使用。通过在assets/fonts目录下放置字体文件,然后使用Typeface类加载,可以轻松更改应用中的字体样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

两种方法:

方法一:

在assets目录下新建目录fonts,然后存放自己的字体库,我这里是微软雅黑weiruanyahei.ttf。

TextView tv;

Typeface tf2 = Typeface.createFromAsset(getAssets(),"fonts/weiruanyahei.ttf");
tv.setTypeface(tf2);


方法二:建立基类,xml统一使用

package com.insight.androidfont;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class FontTextView extends TextView {
    private Context mContext;
    private String TypefaceName = "";


    public String getTypefaceName() {
        return TypefaceName;
    }

    public void setTypefaceName(String typefaceName) {
        TypefaceName = typefaceName;
        Typeface typeface = Typeface.createFromAsset(mContext.getAssets(), "fonts/" + TypefaceName + ".ttf");
        this.setTypeface(typeface);
        System.gc();
    }

    public FontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.mContext = context;
        int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);
        if (resouceId != 0) {
            TypefaceName = context.getResources().getString(resouceId);
        } else {
            TypefaceName = attrs.getAttributeValue(null, "typefaceName");
        }
        if (TypefaceName != null && !"".equals(TypefaceName)) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");
            this.setTypeface(typeface);
        }
    }


    public FontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        // 先判断是否配置的资源文件
        int resouceId = attrs.getAttributeResourceValue(null, "typefaceName", 0);
        if (resouceId != 0) {
            TypefaceName = context.getResources().getString(resouceId);
        } else {
            TypefaceName = attrs.getAttributeValue(null, "typefaceName");
        }
        if (TypefaceName != null && !"".equals(TypefaceName)) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");
            this.setTypeface(typeface);
        }
    }


    public FontTextView(Context context) {
        super(context);
        this.mContext = context;
        // TypefaceName = attrs.getAttributeValue(null, "TypefaceName");
        if (TypefaceName != null && !"".equals(TypefaceName)) {
            Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TypefaceName + ".ttf");
            this.setTypeface(typeface);
        }
    }
}


下面是布局:

<com.insight.androidfont.FontTextView
        android:id="@+id/fontTextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="69dp"
        typefaceName="weiruanyahei"
        android:text="FontTextView"
        android:textSize="30sp" />
大字体是要设置的属性,weiruanyahei是字体库的名字


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值