设置自定义字体通过xml [英] set custom font through xml

查看:642
本文介绍了设置自定义字体通过xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能设置字体,它的TTF驻留在我的资产文件夹通过xml? 我知道如何做到这一点编程但你怎么能做到这一点通过XML?先谢谢了。

how can i set a font, whose ttf resides in my assets folder through xml? I know how to do that programmatically but how can you do that via xml? Thanks in advance.

推荐答案

直接使用XML,但你可以扩展的TextView 并设置默认字体你不能做到这一点。

You cannot do it using XML directly, however you can extend TextView and set a default font.

package com.nannu;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class NanTV extends TextView{

    private Context c;
    public NanTV(Context c) {
        super(c);
        this.c = c;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }
    public NanTV(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);
        // TODO Auto-generated constructor stub
    }

    public NanTV(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.c = context;
        Typeface tfs = Typeface.createFromAsset(c.getAssets(),
                "font/yourfont.ttf");
        setTypeface(tfs);

    }


}

而在布局中使用新的的TextView

<com.nannu.NanTV
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" 

        />

我从我的$ P $光伏答案<一张贴本href="http://stackoverflow.com/a/11239305/1166537">http://stackoverflow.com/a/11239305/1166537

这篇关于设置自定义字体通过xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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