【发布时间】:2013-05-30 22:00:37
【问题描述】:
我有一个关于向 textview 添加多个自定义字体的问题。 我基本上已经在字体文件夹中添加了字体,并根据我在网上找到的解决方案为 fonttextview 创建了一个 java 类。但是我看到他们只添加了一种字体,我想添加多种字体,如roboto-regular、roboto-bold、cabin-bold 等。这是我到目前为止的代码:
public class FontTextView extends TextView {
public FontTextView(Context context) {
super(context);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
public FontTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Typeface face=Typeface.createFromAsset(context.getAssets(), "fonts/Roboto-Bold.ttf");
this.setTypeface(face);
}
如何创建多种字体?另外,我尝试了 styleable 等,但它显示错误,因为它不支持 styleable 类,任何人都可以在现有代码中添加另一种字体并引导我完成检索过程吗?
谢谢!贾斯汀
【问题讨论】:
-
你想同时使用不同的字体??
-
我想使用一些 if else 构造来使用特定的字体,但在同一个类中定义它。另外,我想通过我的 xml 文件中的“id”来调用 if else 构造中的分词字体
-
我已经发布了一个答案,我想这就是你要问的。
-
谢谢,快速提问,你能告诉我如何解决样式问题,以及当你提到 main 和添加 attrs.xml 的资源文件时在哪里包含 xlmns 我是否必须特别需要如您所见,添加字体名称及其通用类型? “字符串”
-
xlmns 将被添加到有
xmlns:android="http://schemas.android.com/apk/res/android"的顶部布局中,紧随其后。
标签: android android-layout android-intent android-emulator android-listview