Welcome 微信登录

首页 / 操作系统 / Linux / Android中字体的处理

1、Android系统默认支持三种字体,分别为:“sans”, “serif”,  “monospace2、在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/目录下,www.linuxidc.com创建Typeface对象
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");         //应用字体
        textView.setTypeface(typeFace);
    }
}