有没有办法在不包含 Roboto 字体的情况下使用 Roboto
资产?
不,对于 API 11
我通常为 Robot 字体创建一个自定义 TextView:
public class TextView_Roboto extends TextView {
public TextView_Roboto(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
createFont();
}
public TextView_Roboto(Context context, AttributeSet attrs) {
super(context, attrs);
createFont();
}
public TextView_Roboto(Context context) {
super(context);
createFont();
}
public void createFont() {
Typeface font = Typeface.createFromAsset(getContext().getAssets(), "robo_font.ttf");
setTypeface(font);
}
}
现在您可以在布局中使用它,如下所示:
<com.my.package.TextView_Roboto>
android:layout_width="..."
android:layout_height="..."
[...]
</com.my.package.TextView_Roboto>
当然,您可以创建 TextView 布局。一种用于 Pre HC,一种用于 HC 及更高版本(您必须使用布局和布局 v11 文件夹)。现在您可以使用<include>
标签将 TextView 包含在布局中。你只需要这样做然后使用这个:
if (android.os.Build.VERSION.SDK_INT >= 11){
TextView txt = (TextView) findViewById(R.id.myTxtView);
}
else{
TextView_Roboto txt = (TextView_Roboto) findViewById(R.id.myTxtView);
}
Edit:
您可以从 Android 4.1+ 开始使用 Roboto,如下所示:
android:fontFamily="sans-serif" // roboto regular
android:fontFamily="sans-serif-light" // roboto light
android:fontFamily="sans-serif-condensed" // roboto condensed