【问题标题】:Android custom font loading errorAndroid自定义字体加载错误
【发布时间】:2014-11-14 15:21:37
【问题描述】:

我尝试在文本视图中使用机器人黑色字体,但 tx.setTypeface(tf); 抛出错误。上面写着“Syntax error on token(s), misplaced construct(s)”和(tf)Syntax error on token "tf", VariableDeclaratorId expected after this token”。

这是我用来更改字体的代码片段:

TextView tx = (TextView) findViewById(R.id.moomoo);
Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Black.ttf");
tx.setTypeface(tf);

据我所知,我只需要更改 java 文件,同时将字体 ttf 文件放在我的资产文件夹中,在名为“字体”的资产文件夹的子文件夹中。然后我只需要一个对文本视图的引用,并且可以从 java 类的构造函数中为我试图更改的 xml 文件的字体执行所有操作,对吗?我的逻辑有缺陷吗,我做错了什么? Android 是 19 版。

【问题讨论】:

  • 你的 tf 不是 null 吗?

标签: android android-fonts


【解决方案1】:

改变

Typeface tf = Typeface.createFromAsset(getAssets(), "Roboto-Black.ttf");

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Black.ttf");

【讨论】:

  • 这很有帮助,而且我的 Android 应用程序的构造中也没有它,我把它放在构造函数的下面,忘记包含它,但移动了括号。再次感谢您的帮助!
猜你喜欢
  • 2011-09-14
  • 2018-02-23
  • 2018-03-18
  • 2014-01-27
  • 2018-08-09
  • 1970-01-01
  • 2022-12-11
  • 2017-01-06
相关资源
最近更新 更多