Android 自定义字体

本文介绍如何在Android应用中设置字体,包括系统自带的sans、serif和monospace字体的使用方法,以及如何加载自定义字体文件,并通过Java代码实现自定义字体的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在android系统中,自带的只有三种字体---"sans","serif"和"monospace",你只要在xml文件中的

android:typeface 

android:typeface属性使用它们:

<TableLayout  
    xmlns:android="
http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:stretchColumns="1">  
    <TableRow>  
        <TextView  
            android:text="sans:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/sans"  
            android:text="Hello, world!"  
            android:typeface="sans"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="serif:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/serif"  
            android:text="Hello, world!"  
            android:typeface="serif"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="monospace:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/monospace"  
            android:text="Hello, world!"  
            android:typeface="monospace"  
            android:textSize="20sp"  
        />  
    </TableRow>  
    <TableRow>  
        <TextView  
            android:text="Custom:"  
            android:layout_marginRight="4px"  
            android:textSize="20sp"  
        />  
        <TextView  
            android:id="@+id/custom"  
            android:text="Hello, world!"  
            android:textSize="20sp"  
        />  
    </TableRow>  
</TableLayout> 


    有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在assets目录下.自定义字体文件不能使用xml代码读取而应该使用java代码:


public class Test extends Activity {   
    @Override  
    public void onCreate(Bundle icicle) {   
        super.onCreate(icicle);   
        setContentView(R.layout.main);   
           
        TextView tv=(TextView)findViewById(R.id.custom);   
        Typeface face=Typeface.createFromAsset(getAssets(),   
                                                                                        "fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取   
           
        tv.setTypeface(face);   
    }   

TTF文件下载:Windows.zip | MAC.zip(选择对应的系统)

    注意:如果你下载的字体Android不支持,那么系统会使用默认字体"sans"。由于应用自定义字体需要额外空间,所以一般不是都需要自定义字体的. 
 

原文地址:http://www.androidmi.com/Androidkaifa/jinjie/201011/1632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值