【发布时间】:2014-07-21 19:47:58
【问题描述】:
我想在我的 android 应用程序中为所有视图使用 Roboto 字体,我该怎么做?
【问题讨论】:
-
在下面的答案中,使用 Typeface createfromasset 时要注意内存泄漏。搜索那个问题。你的问题之前也有报道过。
标签: android
我想在我的 android 应用程序中为所有视图使用 Roboto 字体,我该怎么做?
【问题讨论】:
标签: android
创建自定义视图,例如 TextView、EditText 等。然后在构造函数中设置下面的 setTypeface 示例:
public class RobotoTextView extends TextView {
public RobotoTextView(Context context) {
super(context);
setTypeface(Typeface
.createFromAsset(context.getAssets(), "fontname.ttf"));
}
}
然后在你的布局中使用它,而不是使用标准的 TextView 使用下面的自定义类示例:
<packagename.RobotoTextView
style="@style/usecustomstyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/yourid"/>
【讨论】:
使用字体。将您的字体文件放在 assest 文件夹中。然后写下面的代码。
Typeface Roboto = Typeface.createFromAsset(getAssets(), "Roboto.ttf");
然后使用 Roboto 字体你所有的TextView,EditText..etc
Textview.setTypeface(italic);
【讨论】: