Android 中的自定义字体 [英] Custom Fonts in Android

查看:30
本文介绍了Android 中的自定义字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了一些文章并在谷歌上搜索过,但我没有做到.

I have already read some articles and searched on Google, but I failed to do it.

我的问题是关于字体.

在Android中,"android:typeface"中只有4个属性:Normal、Sans、Serif、Monospace.

In Android, there are only 4 attributes in "android:typeface": Normal, Sans, Serif, Monospace.

那么我必须做什么才能在我的应用程序中使用Verdana"?

So what do I have to do to use "Verdana" in my application?

请建议我在我的 Android 应用程序中使用这种字体的正确方法.

Please suggest me a correct way to use this font in my Android application.

推荐答案

这是一个简单的例子...在项目的根目录中创建一个名为 assets/fonts/ 的文件夹,然后粘贴 TTF字体文件(在本例中为 Verdana.ttf).然后,如果您想将该字体应用于 TextView,请执行以下操作:

This is a simple example... create a folder in the root of your project called assets/fonts/ then paste the TTF font file (in this case Verdana.ttf). Then, if you want to apply that font to, say a TextView, do the following:

import android.graphics.Typeface;

public class FontSampler extends Activity {
  @Override
  public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.main);

    TextView tv=(TextView)findViewById(R.id.custom);
    Typeface face=Typeface.createFromAsset(getAssets(),
                                          "fonts/Verdana.ttf");

    tv.setTypeface(face);
  }
}

此示例取自 ComonsWare 一书(由 Mark Murphy 编写).您可以从 GitHub 下载完整示例.

This example was taken from the ComonsWare book (written by Mark Murphy). You can download the full example from GitHub.

这篇关于Android 中的自定义字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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