[英]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.