如何在android项目中使用Roboto字体 [英] How to use Roboto font in android Project

查看:25
本文介绍了如何在android项目中使用Roboto字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发了一个安卓项目.在这个项目中,文本字体默认为 android:sans.

现在我想将整个项目的默认文本字体替换为机器人字体.

我该怎么做?

解决方案

您可以从这里下载 Roboto 字体:https://fonts.google.com/specimen/Roboto.[2020-01-28 更新]

您可以使用 TypeFace 以常规方式进行操作,如下所示:

Typeface typeface = Typeface.createFromAsset(getAssets(), fontName);textView.setTypeface(字体);

注意:上述内容必须在每个Activity中完成.

或者,例如,如果您想将 Roboto 字体应用于应用程序中的所有 TextView,那么您将需要创建自己的扩展 TextView 的小部件代码>.

有一种简单的方法可以做到这一点.按照这个答案中的步骤操作:https://stackoverflow.com/a/9199258/450534(完整的道具leocadiotine 用于解决方案.我以前用过它,效果很好)>

your_namespace 视为一个标记,您可以为其指定一个您选择的名称.例如,在 XML 中集成 Admob 时,我使用 xmlns:ads.例如,您可以使用:xmlns:font 或一些描述性内容.

至于 custom.ttf 代表什么,它基本上是您需要复制到 Assets 文件夹中的带有扩展名的字体文件.例如,如果您使用的是 ROBOTO-REGULAR.TTF,则将 custom.ttf 替换为 ROBOTO-REGULAR.TTF.使用这个例子,整个代码应该是这样的:

I have developed one android project. In this project the text font defaults to android:sans.

Now I want to replace the default text font to roboto font for my entire project.

How can I do this?

解决方案

You can download the Roboto font from here: https://fonts.google.com/specimen/Roboto. [Updated 2020-01-28]

You can do it the conventional way by using TypeFace, like this:

Typeface typeface = Typeface.createFromAsset(getAssets(), fontName);
textView.setTypeface(typeface);

Note: The above will have to be done in every Activity.

Alternatively, if, for example, you want to apply the Roboto font to all the TextViews in your application, then you will need to create your own widget that extends TextView.

There is a simple way of doing this. Follow the steps from this answer on SO: https://stackoverflow.com/a/9199258/450534 (full props to leocadiotine for the solution. I have used it before and it works like a charm)

EDIT: Think of your_namespace as a marker for you to give it a name of your choice. For example, when integrating Admob in XML, I use xmlns:ads. You can use, for example: xmlns:font or something descriptive.

As for what the custom.ttf stands for, it is basically the font file with its extension that you need to copy in your Assets folder. For example, if you are using ROBOTO-REGULAR.TTF, then replace the custom.ttf with ROBOTO-REGULAR.TTF. Using this example, the entire code should look this this:

<your.package.widget.TypefacedTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:font="http://schemas.android.com/apk/res/your.package"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Custom fonts in XML are easy"
    android:textColor="#FFF"
    android:textSize="14dip"
    font:typeface="ROBOTO-REGULAR.TTF" />

这篇关于如何在android项目中使用Roboto字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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