zl程序教程

您现在的位置是:首页 >  移动开发

当前栏目

Android字体设置及Roboto字体使用方法

Android方法 使用 设置 字体
2023-06-13 09:15:41 时间

本文实例讲述了Android字体设置及Roboto字体使用方法。分享给大家供大家参考。具体分析如下:

一、自定义字体

1.androidTypeface使用TTF字体文件设置字体

我们可以在程序中放入ttf字体文件,在程序中使用Typeface设置字体。
第一步,在assets目录下新建fonts目录,把ttf字体文件放到这。
第二步,程序中调用:

复制代码代码如下:
AssetManagermgr=getAssets();//得到AssetManager
Typefacetf=Typeface.createFromAsset(mgr,"fonts/ttf.ttf");//根据路径得到Typeface
tv=findViewById(R.id.textview);
tv.setTypeface(tf);//设置字体

2.在xml文件中使用android:textStyle=”bold”可以将英文设置成粗体,但是不能将中文设置成粗体,
将中文设置成粗体的方法是:

复制代码代码如下:
TextViewtv=(TextView)findViewById(R.id.TextView01);  
tv.getPaint().setFakeBoldText(true);//中文仿“粗体”--使用TextPaint的仿“粗体”设置setFakeBoldText为true。

注意:部分字体中文无效,虽然不会报错,但是对中文无效。

二、使用RoBoto

自从Android4.0后默认字体就使用了Roboto,下面介绍一下使用方法:

复制代码代码如下:android:fontFamily="sans-serif"//robotoregular 
android:fontFamily="sans-serif-light"//robotolight 
android:fontFamily="sans-serif-condensed"//robotocondensed 
android:fontFamily="sans-serif-thin"//robotothin(android4.2) 
//incombinationwith 
android:textStyle="normal|bold|italic"
可用的参数如下:

Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensedregular
Condenseditalic
Condensedbold
Condensedbold-italic

希望本文所述对大家的Android程序设计有所帮助。