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

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

问题描述

我已经开发了Android项目。在这个项目中的文字字体默认为安卓。SANS

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

现在我要替换默认文本字体roboto字体为我的整个项目。

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

我怎样才能做到这一点?

How can I do this?

推荐答案

您可以从这里下载Roboto字体: <一href="https://www.google.com/design/spec/resources/roboto-noto-fonts.html">https://www.google.com/design/spec/resources/roboto-noto-fonts.html.

You can download the Roboto font from here: https://www.google.com/design/spec/resources/roboto-noto-fonts.html.

您可以使用字体,这样做的conventionaly方式

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

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

注:以上将不得不在每一个活动做

另外,如果,例如,你想要的Roboto字体适用于所有的 TextView中的在你的应用程序,那么您将需要创建自己的小部件,扩展的TextView

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

有这样做的一个简单的方法。按照此回答上,这样的步骤: http://stackoverflow.com/a/9199258/450534 (全道具 leocadiotine 的解决方案。我以前用过它,它就像一个魅力)

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

编辑:想想 your_namespace 作为标记,你给它您选择的名称。例如,当XML集成Admob的,我用的xmlns:广告。您可以使用,例如:的xmlns:字体或一些描述性的

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.

至于什么的 custom.ttf 的代表,它基本上是与它的扩展名的字体文件,你需要在你的资产复制夹。例如,如果您使用的是 ROBOTO-REGULAR.TTF ,然后替换的 custom.ttf 的用的 ROBOTO-REGULAR.TTF 的。使用这个例子中,整个code应该是这样的:

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" />

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

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