如何在Android XML中使用自定义字体?

26

我该如何在xml中使用添加到asset文件夹中的自定义字体?我知道我们可以在java中使用setTypeface()方法,但这样做需要在每个使用该TextView的地方都设置一次。那么有更好的方法吗?


嗨,请参考这篇文章https://dev59.com/r2855IYBdhLWcg3wXC1-。那里讨论并回答了一个相同性质的问题。 - Wajeeh
尝试一下这个教程:http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/,我认为它会对你有所帮助。 - Ajay
我已经更新了我的回答,请从那个答案中删除“-ve”。 - Shreyash Mahajan
2个回答

58

通过谷歌搜索,我发现最好的方法是- 假设你想在TextView中使用字体,那么我们需要扩展TextView并在其中设置字体,然后我们可以在xml中使用我们自定义的TextView。 下面我将展示扩展的TextView

package com.vins.test;

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

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();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "your_font.ttf");
        setTypeface(tf);
    }

}

我们在每个构造函数中调用init()来设置字体。 稍后,我们必须像下面展示的那样在main.xml中使用它。

<com.vins.test.MyTextView
    android:id="@+id/txt"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="This is a text view with the font u had set in MyTextView class "
    android:textSize="30dip"
    android:textColor="#ff0000"
   >

更新:

请注意,根据 pandre 的提醒,早于4.0版本的 Android 存在内存泄漏问题。


1
@vins - 谢谢。这是否意味着ttf文件包括字体的所有子类型-粗体、斜体等以及它们的组合? - AlikElzin-kilaka
4
请注意,此解决方案会在旧版 Android 中导致内存泄漏。通过缓存所创建的 Typeface 来避免这种情况。更多信息请参见:https://code.google.com/p/android/issues/detail?id=9904 - pandre
1
这个方法是可行的。但是,这个自定义textview的膨胀需要200毫秒,而默认的textview只需要2毫秒。小心使用这种方法 :( - nizam.sp
嘿 @nizam.sp,你有相关的参考资料吗? - Antwan
@Tony:所花费的时间是由于pandre提到的内存泄漏问题导致的。 - nizam.sp
显示剩余7条评论

2
请将字体文件放在asset\fonts\fontname中。
在xml文件中定义三个TextView,然后将以下代码放入您的activity类中:
public class AndroidExternalFontsActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Font path
        String fontPath = "fonts/DS-DIGIT.TTF";
        String fontPath1 = "fonts/Face Your Fears.ttf";
        String fontPath2 = "fonts/HelveticaNeue-Bold_0.otf";

        // text view label
        TextView txtGhost = (TextView) findViewById(R.id.ghost);
        TextView txtGhost1 = (TextView) findViewById(R.id.ghost1);
        TextView txtGhost2 = (TextView) findViewById(R.id.ghost2);

        // Loading Font Face
        Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
        Typeface tf1 = Typeface.createFromAsset(getAssets(), fontPath1);
        Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath2);

        // Applying font
        txtGhost.setTypeface(tf);
        txtGhost1.setTypeface(tf1);
        txtGhost2.setTypeface(tf2);
    }
}

13
这是设置字体的正常方式,问题是如何在XML中默认使用自定义字体。 - Vins

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接