在android系统中,自带的只有三种字体---"sans","serif"和"monospace",你只要在xml文件中的

android:typeface

android:typeface属性使用它们:

<TableLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="sans:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/sans"
android:text="Hello, world!"
android:typeface="sans"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="serif:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/serif"
android:text="Hello, world!"
android:typeface="serif"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="monospace:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/monospace"
android:text="Hello, world!"
android:typeface="monospace"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="Custom:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/custom"
android:text="Hello, world!"
android:textSize="20sp"
/>
</TableRow>
</TableLayout>

<TableLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="sans:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/sans"
android:text="Hello, world!"
android:typeface="sans"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="serif:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/serif"
android:text="Hello, world!"
android:typeface="serif"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="monospace:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/monospace"
android:text="Hello, world!"
android:typeface="monospace"
android:textSize="20sp"
/>
</TableRow>
<TableRow>
<TextView
android:text="Custom:"
android:layout_marginRight="4px"
android:textSize="20sp"
/>
<TextView
android:id="@+id/custom"
android:text="Hello, world!"
android:textSize="20sp"
/>
</TableRow>
</TableLayout>

有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在assets目录下.自定义字体文件不能使用xml代码读取而应该使用java代码:


public class Test 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/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取

tv.setTypeface(face);
}
}

public class Test 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/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取

tv.setTypeface(face);
}
}

TTF文件下载:Windows.zip|MAC.zip(选择对应的系统)

注意:如果你下载的字体Android不支持,那么系统会使用默认字体"sans"。由于应用自定义字体需要额外空间,所以一般不是都需要自定义字体的.

转载:http://www.androidmi.com/Androidkaifa/jinjie/201011/1632.html

更多相关文章

  1. Android(二)HelloWorld,Android(上)
  2. Android7.0升级版本适配问题
  3. android学习之textView布局文件
  4. Android构建音频播放器教程(四)
  5. TextView 不用 ScrollViewe 也可以滚动
  6. 在Android(安卓)Studio中使用Android(安卓)Tests(Junit)
  7. Android(安卓)获取ip地址
  8. Android(安卓)替换全局字体
  9. Android(安卓)5.1 SDK下载与配置

随机推荐

  1. MySql查询脚本,每月统计活动用户。
  2. php怎么读取MYSQL数据到radio选项
  3. 计算他们自动放入全列的两列
  4. mysql安装后不能重新修改目录文件夹的名
  5. mysql 常用字段和占用 字节数
  6. mysql数据库拷贝到另一个服务器上
  7. 是否会添加不必要的比较以获得结果但是减
  8. mysql-5.7.22-winx64 mysql 安装笔记
  9. 如何在产品和类别应用程序树中将1个表连
  10. myisam 表中, 删除数据后运行 OPTIMIZE T