Android 更改申请表

Android 更改申请表,android,textview,android-fonts,Android,Textview,Android Fonts,我有一个应用程序,我想改变整个应用程序中文本的字体。 是否可以通过编程方式更改应用程序的字体或在清单中使用xml更改应用程序的字体?最简单的方法是创建自己的文本视图: public class MyTextView extends TextView { public DMTextView(Context context, AttributeSet attrs) { super(context, attrs); // you need your TypefaceF

我有一个应用程序,我想改变整个应用程序中文本的字体。

是否可以通过编程方式更改应用程序的字体或在清单中使用xml更改应用程序的字体?

最简单的方法是创建自己的文本视图:

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中的任何位置使用它:
就像普通的文本视图一样

可以通过缓存字体来改进它,这样您就不必在每次引用TextView时都重新创建它。

试试这个

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

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

tv.setTypeface(font);
2.通过xml进行设置


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

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

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

在asset中创建字体文件夹,并粘贴您想要粘贴的字体

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

public enum TypeSafe {
   HELVETICANEUELTCOMBD,
   HELVETICANEUELTCOMBDCN,
   HELVETICANEUELTCOMCN,
}
之后,在
活动中创建一个方法,或者如果您有
实用程序

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);
    } 
}
活动中调用这些方法

setTypeface(yourtextView, TypeSafe.HELVETICANEUELTCOMLT, getAssets());

我相信这已经被回答了n次了。请浏览论坛。真的很高兴否决一个答案,但不告诉为什么。如果你不告诉我,我怎么知道两年来我做错了什么(而且一直工作得很好)?!多谢了,小家伙。