gpt4 book ai didi

android - 如何在android应用程序中使用自定义字体

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:32 25 4
gpt4 key购买 nike

为了实现自定义字体,我在这里看到了几个例子,问题是不同的,我在一个抽象类中使用自定义字体,它在整个应用程序中使用,这是我的代码

 public abstract class X extends Activity implements OnClickListener {

private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
public static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
}

通过调试尝试这里我得到空指针异常

Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);

如何解决这个问题给出一些建议。

最佳答案

在activity的onCreate方法中创建Typeface对象

public abstract class X extends Activity implements OnClickListener {

private Vibrator vibrator;
private TextView TV_score;
private TextView TV_hints;
private ImageButton BTN_back;
// Font path
private String fontPath = "fonts/CarterOne.ttf";
Typeface tf ;
public static Context context;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Logger.log("onCreate " + this.getClass().getName());
vibrator = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
context = getBaseContext();
SoundHandler.getInstance().initSounds(context);
tf = Typeface.createFromAsset(getAssets(), fontPath);
}

关于android - 如何在android应用程序中使用自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22878302/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com