使用Roboto字体为早期设备 [英] Use Roboto font for earlier devices

查看:140
本文介绍了使用Roboto字体为早期设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用的Roboto字体在我的Andr​​oid应用程序,并确保它适用于早期版本的Andr​​oid系统没有安装的字体。我知道我可以用Typeface.createFromAsset(这样做),然后手动设置字体,我的每个TextViews /按钮/其它对象。这似乎是一个很大的痛苦要做到这一点,因为我在屏幕上显示,虽然每个对象。

I would like to use the Roboto font in my Android application and make sure it works for earlier versions of Android that don't have the font installed. I know I can do this by using Typeface.createFromAsset() and then manually setting the font for each of my TextViews/Buttons/Other-Objects. It seems like a big pain to do this for every object I show on the screen though.

我的问题是,有没有更好的方式来做到这一点?一些辅助类或方法来设置一个主题的.xml文件自定义字体?任何自动会比手工列出​​了每个屏幕上的每一个对象,并更改字体更好。

My question is, is there a better way to do this? Some helper class or a way to set a custom font in a .xml theme file? Anything automated would be better than manually listing out every object on each screen and changing the font.

谢谢!

推荐答案

检索内活动的所有意见,检查它的类型,并采取适当的行动。

Retrieve all views inside activity, check its type and apply appropriate action.

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/Roboto/Roboto-Regular.ttf");
for (View view : allViews)
{
 if (view instanceof TextView) 
 {
    TextView textView = (TextView) view;
    textView.setTypeface(typeface);
  }
}

这篇关于使用Roboto字体为早期设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆