【发布时间】:2017-08-06 12:46:00
【问题描述】:
我是android的初学者,我想改变一些控制字体。
第一种方式:
TextView tx = (TextView)findViewById(R.id.textview1);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/abc.ttf");
tx.setTypeface(custom_font)
这种方式不好,因为你必须找到所有控件来设置字体
第二种方式: 创建从目标控件扩展的自定义控件
public class TextViewWithFont extends TextView {
public TextViewWithFont(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/abc.ttf")
this.setTypeface(custom_font);
}
public TextViewWithFont(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/abc.ttf")
this.setTypeface(custom_font);
}
public TextViewWithFont(Context context) {
super(context);
Typeface custom_font = Typeface.createFromAsset(getAssets(), "fonts/abc.ttf")
this.setTypeface(custom_font);
}
第三种方式:使用书法库
所以,伙计们,你有什么想法?哪个更好,你知道改变字体的其他方法吗?
【问题讨论】:
-
我通过在 Application 类中创建和初始化一个静态 TypeFace 来做到这一点。然后在任何地方使用它作为 tx.setTypeface(custom_font)
-
你想在布局 XML 中也设置这个字体还是只在 Java 中设置这个字体?
-
@MuthukrishnanRajendran 感谢您的回复.. 我想在布局 xml 中使用
-
@NishinRaj 感谢您的回复。你能举个例子说明你是做什么的吗?
-
@hosseinderakhshan 无法将代码添加为注释,因为它不清楚。所以我将其添加为答案。
标签: android fonts custom-font