一:Android系统默认支持三种字体,分别为:“sans, serif, monospace"

二:

main.xml代码

<?xml version="1.0"encoding="utf-8"?>

<TableLayoutxmlns:Android="http://schemas.android.com/apk/res/android"
Android:layout_width="fill_parent"
Android:layout_height="fill_parent">
<TableRow>
<TextView Android:text="sans:"
Android:layout_marginRight="4px"
Android:textSize="20sp"></TextView>

<!-- 使用默认的sans字体-->
<TextView Android:id="@+id/sans"
Android:text="Hello,World"
Android:typeface="sans"
Android:textSize="20sp"></TextView>
</TableRow>
<TableRow>
<TextView Android:text="serif:"
Android:layout_marginRight="4px"
Android:textSize="20sp"></TextView>

<!-- 使用默认的serifs字体-->
<TextViewAndroid:id="@+id/serif"
Android:text="Hello,World"
Android:typeface="serif"
Android:textSize="20sp"></TextView>
</TableRow>
<TableRow>
<TextView Android:text="monospace:"
Android:layout_marginRight="4px"
Android:textSize="20sp"></TextView>

<!-- 使用默认的monospace字体-->
<TextViewAndroid:id="@+id/monospace"
Android:text="Hello,World"
Android:typeface="monospace"
Android:textSize="20sp"></TextView>
</TableRow>

<!-- 这里没有设定字体,我们将在Java代码中设定-->
<TableRow>
<TextView Android:text="custom:"
Android:layout_marginRight="4px"
Android:textSize="20sp"></TextView>
<TextViewAndroid:id="@+id/custom"
Android:text="Hello,World"
Android:textSize="20sp"></TextView>
</TableRow>
</TableLayout>

[代码]FontsActivity.java

package yyl.fonts;

import Android.app.Activity;
import Android.graphics.Typeface;
import Android.os.Bundle;
import Android.widget.TextView;

public class FontsActivity extends Activity {
/** Called when the activity is firstcreated. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//
得到TextView控件对象
TextView textView =(TextView)findViewById(R.id.custom);

//将字体文件保存在assets/fonts/目录下,www.linuxidc.com创建Typeface对象
Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/HandmadeTypewriter.ttf");

//应用字体
textView.setTypeface(typeFace);
}
}

更多相关文章

  1. BitMap、Drawable、inputStream及byte[] 互转
  2. android 跨应用跳转 启动其他应用指定界面
  3. 中:Android(安卓)startActivity原理分析(基于Android(安卓)8.1 AO
  4. Android(安卓)之 自定义控件用法介绍
  5. AsyncTaskLoader使用方法
  6. Android单元测试笔记
  7. android 更改TextView部分字体颜色、大小、背景
  8. 三行Android代码实现白天夜间模式流畅切换
  9. Android(安卓)一键锁屏实现

随机推荐

  1. 《趣说前端 - 002 》— 两种作用域模型介
  2. 贞炸了!上线之后,消息收不到了!
  3. 【干货】BAT大佬告诉你如何埋点
  4. Vuex 注入 Vue 生命周期的过程
  5. 熔断器 Hystrix 源码解析 —— 请求执行(
  6. 数据库读写分离这个坑,让刚入职的我一脸懵
  7. 学习C第二天-字符串
  8. centos 磁盘管理与维护
  9. 【前端词典】arr.filter(parseInt) 考查
  10. 这些有趣的 API 着实有点炫