2012-01-27 134 views
24

如何使用在xml中的资产文件夹中添加的自定义字体?我知道我们可以在java中使用setTypeface()方法,但是我们必须在使用TextView的任何地方执行此操作。那么有没有更好的方法?如何在android xml中使用自定义字体?

+0

我已经更新我的答案。请从该答案中删除。 – 2012-01-27 10:42:38

+0

试用本教程[http://www.barebonescoder.com/2010/05/android-development-using-custom-fonts/](http://www.barebonescoder.com/2010/05/android-development-using -custom-fonts /)我认为它可以帮助你 – Ajay 2012-01-27 10:15:53

+0

嗨请参考这篇文章。在那边讨论并回答了同样性质的问题。 – Wajeeh 2012-01-27 07:37:25

回答

58

我发现的最好的方法是使用Google搜索 - 说如果你想在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" 
    > 

更新:

注意有关在4.0版本之前的Android的内存泄漏由pandre提及。

+0

@vins - 如果用户还说'android:textStyle =“bold”'?文本确实会设置为粗体? – 2013-08-02 12:14:50

+0

@ kilaka是的,它应该变成粗体。 – Vins 2013-08-06 04:25:26

+1

@vins - 谢谢。这是否意味着ttf文件包含字体的所有子类型 - 粗体,斜体等及其组合? – 2013-08-06 08:06:57

2

把你的字体文件在asset\fonts\fontname

在XML文件中定义三个TextView的话,把这个代码在你的活动类:

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 2013-08-06 05:46:11

相关问题