在 API 级别最低为 14 的应用中使用 Roboto 字体

2024-02-28

我有一个应用程序,其最低 API 级别为 14。我认为所有兼容设备都应默认安装 Roboto 字体,这种想法正确吗?如果我将 textView 字体设置为 Roboto 或 Roboto Light,它似乎默认为正常的无字体。

有没有办法在不将 Roboto 字体包含为资产的情况下使用 Roboto?


有没有办法在不包含 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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在 API 级别最低为 14 的应用中使用 Roboto 字体 的相关文章

随机推荐