如何在.xml文件而不是.java文件中设置自定义字体? [英] How to set custom font in .xml file instead of .java file?

查看:109
本文介绍了如何在.xml文件而不是.java文件中设置自定义字体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个要在其中设置自定义字体的应用程序.

I am creating one Application in which I want to set custom font.

但是我不能在.xml文件中使用自定义字体,为此,我需要初始化.java文件中的每个TextView.

But I can't use custom Fonts in .xml file, for using this I need to initialize each and every TextView in .java file.

此过程非常冗长且耗时.

This process is Too much lengthy and time consuming.

如果有人知道,请帮助我...

If anyone know then please help me...

推荐答案

供您参考,

 public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyTextView(Context context) {
        super(context);
        init();
    }

    public void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "font/yourfont.ttf");
        setTypeface(tf ,1);

    }
}

在XML中,

 <you_package.MyTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:textSize="20sp"
        android:text="Your text"
        />

这篇关于如何在.xml文件而不是.java文件中设置自定义字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆