安卓自定义字体

标签 android typeface

您好,我在我的应用中使用自定义 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/

相关文章:

android - 如果已经从 Android 中的 FB SDK 获得访问 token ,如何在 WebView 中登录 facebook

android - 录制使预览和比率变暗

android - android ListView 中的自定义字体

android - 在 Android 中设置自定义字体样式的正确方法

Android 许可服务 - LicenseChecker 总是超时

Android SDK 构建工具多个版本

java - 选择按钮时我的第二个 Android Activity 不会启动

css - 可变字体和普通字体有什么区别

html - 如何为 three.js 生成支持 Unicode 的字体文件?

android - 如何更改 Textview 中的字母间距?