在Android的自定义字体自定义、字体、Android

2023-09-11 20:08:29 作者:吟留

我已经读了一些文章和搜索谷歌,但我没能做到这一点。

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

我的问题是关于字体面。

My problem is regarding the font-face.

在Android中,只有4属性安卓字体:正常,三世,衬线,等宽

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

那么,做我必须做使用宋体,在我的应用程序?

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

请建议我使用这个字体在我的Andr​​oid应用程序的正确道路。

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

推荐答案

这是一个简单的例子......在你的项目中称为根目录下创建一个文件夹资产/字体/ 然后粘贴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书(作者马克·墨菲)。您可以从GitHub的下载完整的例子。

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

 
精彩推荐