安卓中改變字體

在開發安卓應用時,有時我們要用到一些漂亮的字體來裝飾界面,好的字體也會增加應用的用戶體驗度,但系統默認提供的字體有時並不能滿足我們的要求。

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>

以下是引用外部字體,例如下面的字體是應用了靜蕾字體:

120020ypt5jjrhirrgzh1h.jpg

使用方法爲:

在工程中新建文件夾assets/fonts/,然後把字體文件放到下面,字體,命名要符合系統要求,不符合的重命名修改以下


然後再java程序中使用方法爲:

TextView textView = (TextView) findViewById(R.id.test);
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/jinglei.ttf");
textView.setTypeface(typeFace);

這樣就完成對字體一個TextView字體的修改。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章