简体   繁体   English

Android将Roboto字体设置为粗体,斜体,常规,…(类似于自定义字体系列)

[英]Android set Roboto font with bold, italic, regular,… (something like custom font family)

I know ho to set custom font programmatically inside Android app. 我知道如何在Android应用程序中以编程方式设置自定义字体。 Is there any way to load typeface for custom font (assets) and Android framework will use proper file based on bold, italic and so on? 有什么方法可以为自定义字体(资产)加载字体,Android框架将使用基于粗体,斜体等的正确文件吗?

For example now I'm trying to set Roboto font to some TextView 例如现在我正在尝试将Roboto字体设置为某些TextView

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
textView.setTypeface(typeface);

It works ok. 可以。 But since I set TextView inside xml layout to bold , text is not bolded 但是由于我将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" />

How to load typeface from assets properly that this will work? 如何正确从资产加载字体,这将起作用?

textView.setTypeface(typeface, Typeface.BOLD);

Inside my assets dir there is only one "font family" 在我的资产目录中,只有一个“字体家族”

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

How to load all that fonts inside one typeface/family? 如何在一个字体/家族中加载所有这些字体?

Not sure if it's a good idea to post a link, but anyway... Here is my blog post on this topic, it has a class that solves this problem fully. 不知道发布链接是否是一个好主意,但是无论如何...这是我关于此主题的博客文章,它具有一个可以完全解决此问题的类。 http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/ http://anton.averin.pro/2012/09/12/how-to-use-android-roboto-font-in-honeycomb-and-earlier-versions/

I don't know of a way to treat different typeface variants of a single font as one family, but typefaces tend to have large file sizes so you probably wouldn't want to import all those typefaces into your app anyway. 我不知道将一种字体的不同字体变体作为一个家族来对待的方法,但是字体往往具有较大的文件大小,因此无论如何您可能都不希望将所有这些字体导入到您的应用程序中。 Instead, you could use just the Medium font and then set bold and italic attributes for that. 相反,您可以只使用Medium字体,然后为此设置粗体和斜体属性。

For example, if you have android:textStyle="bold" set in your XML layout, you can do this in your code to keep the bold style: 例如,如果您在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM