安卓中改变字体

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

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>


以下是引用外部字体,例如下面的字体是应用了静蕾字体:



使用方法为:

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


然后再java程序中使用方法为:

TextView textView = (TextView) findViewById(R.id.test);

Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/jinglei.ttf");

textView.setTypeface(typeFace);

这样就完成对字体一个TextView字体的修改。


若发现了错误或有疑问请加群140239644讨论。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值