2012-03-02 224 views
4

我知道豪设置自定义字体编程Android应用程序内。 有没有什么方法来加载自定义字体(资产)的字体和Android框架将使用基于大胆,斜体等适当的文件?的Android设置的Roboto字体,加粗,斜体,普通,...(有点像自定义字体家族)

比如现在我想的Roboto字体设置为某个TextView

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

它工作正常。但因为我设置TextView中的XML布局大胆,文本不加粗

<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 

如何加载所有排字体/家庭里面的字体?

回答

2

我不知道如何将单个字体的不同字体变体作为一个家族来处理,但字体往往会有较大的文件大小,因此您可能不希望将所有这些字体导入到您的应用中。相反,您可以只使用Medium字体,然后为其设置粗体和斜体属性。

举例来说,如果你有安卓TEXTSTYLE =“黑体”在你的XML布局设置,你可以在你的代码,以大胆的风格做到这一点:

Typeface currentTypeFace = textView.getTypeface(); 
if (currentTypeFace != null && currentTypeFace.getStyle() == Typeface.BOLD) { 
    textView.setTypeface(tf, Typeface.BOLD); 
} else { 
    textView.setTypeface(tf); 
}