[英]Android set Roboto font with bold, italic, regular,… (something like custom font family)
我知道如何在Android应用程序中以编程方式设置自定义字体。 有什么方法可以为自定义字体(资产)加载字体,Android框架将使用基于粗体,斜体等的正确文件吗?
例如现在我正在尝试将Roboto字体设置为某些TextView
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);
可以。 但是由于我将xml布局内的TextView
设置为粗体,因此文本未加粗
<TextView
android:id="@+id/my_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="50dp"
android:textStyle="bold"
android:gravity="center"
android:text="@string/my_text"
android:textColor="@color/my_foreground"
android:textSize="24dp" />
如何正确从资产加载字体,这将起作用?
textView.setTypeface(typeface, Typeface.BOLD);
在我的资产目录中,只有一个“字体家族”
Roboto-Black.ttf
Roboto-BlackItalic.ttf
Roboto-Bold.ttf
Roboto-BoldCondensed.ttf
Roboto-BoldCondensedItalic.ttf
Roboto-BoldItalic.ttf
Roboto-Condensed.ttf
Roboto-CondensedItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Medium.ttf
Roboto-MediumItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf
如何在一个字体/家族中加载所有这些字体?
不知道发布链接是否是一个好主意,但是无论如何...这是我关于此主题的博客文章,它具有一个可以完全解决此问题的类。 http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/
我不知道将一种字体的不同字体变体作为一个家族来对待的方法,但是字体往往具有较大的文件大小,因此无论如何您可能都不希望将所有这些字体导入到您的应用程序中。 相反,您可以只使用Medium字体,然后为此设置粗体和斜体属性。
例如,如果您在XML布局中设置了android:textStyle =“ bold”,则可以在代码中执行此操作以保留粗体样式:
Typeface currentTypeFace = textView.getTypeface();
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) {
textView.setTypeface(tf, Typeface.BOLD);
} else {
textView.setTypeface(tf);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.