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

2、在Android中可以引入其他字体

3、示例如下:

4、布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns: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字体-->
<TextView android: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字体-->
<TextView android: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>
<TextView android:id="@+id/custom"
android:text="Hello,World"
android:textSize="20sp"></TextView>
</TableRow>
</TableLayout>

5、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 first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//得到TextView控件对象
TextView textView = (TextView)findViewById(R.id.custom);

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

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

}
}

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. android中的Inflater
  6. Android(安卓)7.0 移除设置中的某些项(辅助功能、流量使用情况、
  7. Android(安卓)ListView里设置默认Item的背景颜色
  8. 第15天android:使用sqlite
  9. GMS Android(安卓)Q移除launcher3 googlesearchbox

随机推荐

  1. android 相对布局属性说明
  2. Android解惑
  3. Android LinearLayout 嵌套 button 监听L
  4. 【Android应用】【Shape使用总结】
  5. 如何调用Android隐藏API
  6. AIDL的实现
  7. ubuntu下Android源代码模块编译
  8. [Android Develop_004] Android Backgrou
  9. Android系统服务概要
  10. Android(安卓)Low Memory Killer