安卓中改变字体

在开发安卓应用时,有时我们要用到一些漂亮的字体来装饰界面,好的字体也会增加应用的用户体验度,但系统默认提供的字体有时并不能满足我们的要求。

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字体的修改。

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