您好,我在我的应用中使用自定义 Typeface
,代码如下:
public Typeface font;
//Activity on create:
font = Typeface.createFromAsset(getAssets(),"DINOT-Medium.otf");
TextView tv = (TextView)vi.findViewById(R.id.textView1);
tv.setTypeface(font);
问题是一段时间后出现错误:无法再读取文本,只能看到方 block 。你知道为什么会这样吗?如何修复? 谢谢
最佳答案
像这样创建一个自定义 TextView
:
public class DINOTMediumTextView extends TextView {
public DINOTMediumTextView(Context context) {
super(context);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs) {
super(context, attrs);
setCustomFont(context);
}
public DINOTMediumTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setCustomFont(context);
}
private void setCustomFont(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fonts/DINOT-Medium.otf");
setTypeface(tf);
}
}
将字体文件放在 assets/fonts/
中(在 assets 文件夹中创建一个文件夹)
然后在您的布局 xml 中:
<com.yourapp.views.DINOTMediumTextView
android:id="blabla"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
com.yourapp.views
是包含您的 DINOTMediumTextView
类的包的名称。
关于安卓自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13604450/