Android提供三种字体:“Sans”,“serif“和“monospace”。 默认:Sans

1、在Android XML文件中设置字体

<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(sans)"
android:typeface="sans" />

<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(serif)"
android:typeface="serif" />

<TextView
android:id="@+id/t3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello_world!您好,世界!(monospace)"
android:typeface="monospace" />

2、自定义字体
  1)将新字体的TTF文件copy到assets/fonts/目录下面。
  2)我们需要将widget设置为该字体,比较遗憾的是,不能直接在XML文件中进行,需要编写源代码。

typeface= Typeface.createFromAsset(getAssets(), "fonts/lz.ttf");

textView.setTypeface(typeface);

3. UI所有组件使用自定义字体

3.1 方法
public static void changeFonts(ViewGroup root, Activity act) {
Typeface tf = Typeface.createFromAsset(act.getAssets(),
"fonts/lz.ttf");
for (int i = 0; i < root.getChildCount(); i++) {
View v = root.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(tf);
} else if (v instanceof Button) {
((Button) v).setTypeface(tf);
} else if (v instanceof EditText) {
((EditText) v).setTypeface(tf);
} else if (v instanceof ViewGroup) {
changeFonts((ViewGroup) v, act);
}
}
}
}
3.2 activity中调用方法

ViewGroup viewGroup=(ViewGroup)findViewById(android.R.id.content);
FontManager.changeFonts(viewGroup, this);

4. html的使用

String str = "测试<b>黑体字</b>、<i>斜体字</i>、<u>下划 线</u>、<font color='red'>红色字</font>的显示。" ;

textView.setText(Html.fromHtml(str));

更多相关文章

  1. 传智播客Android视频教程——第四天
  2. Android(安卓)常用工具
  3. android 根据string 获取资源文件的id
  4. Android(安卓)NDK 的学习之旅-----JNI LOG 打印
  5. Android(安卓)实例子源代码文件下载地址380个合集
  6. android--RelativeLayout布局的一些理解
  7. android adb shell 命令大全
  8. android 布局文件中xmlns:android="http://schemas.android.com/
  9. Android(安卓)资源文件中@、@android:type、@*、?、@+含义和区别

随机推荐

  1. Android开发入门——推箱子游戏开发实战(
  2. android中怎样将文件放入sdcard
  3. Android中如何给layout资源文件分类 在la
  4. Android(安卓)App开机自动启动
  5. Android权限系统(一):开机获取权限信息
  6. Android(安卓)绘制线程动画
  7. Android(安卓)PopupWindow与ListView配合
  8. Android(安卓)ListView 实现 GridView
  9. Android滑动组件----RecyclerView并且实
  10. android 仿iphone主题之主菜单