更改应用程序的FontFace

时间:2013-12-17 09:27:54

标签: android textview android-fonts

我有一个应用程序,我想在整个应用程序中更改文本字体。
无论如何改变应用字体以编程方式或使用 xml in manifest 。?

4 个答案:

答案 0 :(得分:1)

试试这个

1.将您的ttf文件放在assets文件夹中,并将这些行添加到您的java文件

Typeface font = Typeface.createFromAsset(activity.getAssets(),"fonts/androidnation.ttf");

tv.setTypeface(font);

2.通过xml

设置它

XML Typeface

答案 1 :(得分:0)

最简单的方法是创建自己的TextView:

public class MyTextView extends TextView {

    public DMTextView(Context context, AttributeSet attrs) {
      super(context, attrs);
      // you need your TypefaceFile "myTypeface.ttf" in the assets folder of your project
      setTypeface(Typeface.createFromAsset(context.getAssets(),"myTypeface.ttf"));
    }

    // add the other constructors if you want
}

现在,您可以在xml中的任何位置使用它: <com.your.package.MyTextView ... >就像普通的文字视图一样

可以通过缓存字体来改进它,因此您不必在每次引用TextView时再次创建它。

答案 2 :(得分:0)

将字体放在fonts文件夹中,然后使用以下代码。

TextView tv = (TextView) findViewById(R.id.appname);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/epimodem.ttf");
tv.setTypeface(face);

这是以编程方式设置字体的方法。

答案 3 :(得分:0)

在资源中创建 fonts 文件夹,然后粘贴要粘贴的字体。

创建一个类。将其命名为 Typesafe.java

public enum TypeSafe {
   HELVETICANEUELTCOMBD,
   HELVETICANEUELTCOMBDCN,
   HELVETICANEUELTCOMCN,
}

之后,在Activity中创建一个方法,或者如果您拥有Utility类。

public void setTypeface(TextView textView, TypeSafe type, AssetManager assetManager){
    if (TypeSafe.HELVETICANEUELTCOMBD.equals(type)) {
        final Typeface typeface = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-Bd.ttf");
        textView.setTypeface(typeface);
    } else if (TypeSafe.HELVETICANEUELTCOMBDCN.equals(type)) {
        final Typeface typeface1 = Typeface.createFromAsset(assetManager, "fonts/HelveticaNeueLTCom-BdCn.ttf");
        textView.setTypeface(typeface1);
    } 
}

activity

中调用这些方法
setTypeface(yourtextView, TypeSafe.HELVETICANEUELTCOMLT, getAssets());
相关问题