在開發安卓應用時,有時我們要用到一些漂亮的字體來裝飾界面,好的字體也會增加應用的用戶體驗度,但系統默認提供的字體有時並不能滿足我們的要求。
Android系統提供給開發者三種字體,分別爲:“sans”, “serif”, “monospace
以下是這三種字體的使用:
<?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:layout_marginRight="4px" Android:text="sans:" Android:textSize="20sp" > </TextView> <TextView Android:id="@+id/sans" Android:text="Hello,World" Android:textSize="20sp" Android:typeface="sans" > </TextView> </TableRow> <TableRow> <TextView Android:layout_marginRight="4px" Android:text="serif:" Android:textSize="20sp" > </TextView> <TextView Android:id="@+id/serif" Android:text="Hello,World" Android:textSize="20sp" Android:typeface="serif" > </TextView> </TableRow> <TableRow> <TextView Android:layout_marginRight="4px" Android:text="monospace:" Android:textSize="20sp" > </TextView> <TextView Android:id="@+id/monospace" Android:text="Hello,World" Android:textSize="20sp" Android:typeface="monospace" > </TextView> </TableRow> </TableLayout>
以下是引用外部字體,例如下面的字體是應用了靜蕾字體:
TextView textView = (TextView) findViewById(R.id.test); Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/jinglei.ttf"); textView.setTypeface(typeFace);
這樣就完成對字體一個TextView字體的修改。