Android上的自定义字体

Gab*_*eis 15 user-interface android

我想在Android应用程序中使用自定义字体,但使用自定义字体似乎很痛苦?!我可以通过将其添加到每个TextView - >许多样板代码来以编程方式设置自定义字体.我可以创建一个新类CustomTextView扩展TextView并在构造函数中添加setTypeface - >不再有xml上下文感知属性帮助.

还有另一种设置自定义字体的方法吗?按XML,按风格,按主题?

提前致谢!

PS是否有可能通过XML设置下划线?

Kun*_*ary 11

使用此代码更改字体textview ..

TextView newfont;
newfont=(TextView) findViewById(R.id.textView1);
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/DejaVuSerif-Italic.ttf");
newfont.setTypeface(font);
newfont.setText("This is the new font Text");
Run Code Online (Sandbox Code Playgroud)

从web下载DejaVuSerif-Italic.ttf文件并将其放入assets文件夹中的fonts文件夹中.

  • 你显然不明白这个问题. (2认同)

Jon*_*ryl 6

您可以在assets/fonts下包含自定义字体,然后使用以下代码

https://github.com/browep/AndroidCustomFontWidgets/

您可以在XML中指定字体,例如

<com.github.browep.customfonts.view.FontableTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="This is in a custom font"
    app:font="MyFont-Bold.otf" />
Run Code Online (Sandbox Code Playgroud)

该代码支持FontableTextView和FontableButton,但如果需要,可以很容易地扩展以支持其他小部件类型.