Android中通过Typeface设置字体

Android系统默认字体支持四种字体,分别为:

noraml(普通字体,系统默认使用的字体)
sans(非衬线字体)
serif(衬线字体)
monospace(等宽字体)

除此之外还可以使用其他字体文件(*.ttf)

一、使用系统自带的字体(有2种方式)

1.在xml中修改字体。

    <!--使用默认的sans字体-->
    <TextView
        android:id="@+id/sans"
        android:text="Hello,World"
        android:textSize="20sp"
        android:typeface="sans" />

    <!--使用默认的serif字体-->
    <TextView
        android:id="@+id/serif"
        android:text="Hello,World"
        android:textSize="20sp"
        android:typeface="serif" />

    <!--使用默认的monospace字体-->
    <TextView
        android:id="@+id/monospace"
        android:text="Hello,World"
        android:textSize="20sp"
        android:typeface="monospace" />

2.在Java代码中修改字体。

第一步: 获取TextView实例。

  //获取textView实例
  TextView textView = findViewById(R.id.textview);
第二步:设置字体。

   //设置serif字体
   textView.setTypeface(Typeface.SERIF);
   //设置sans字体
   textView.setTypeface(Typeface.SANS_SERIF);
   //设置monospace字体
   textView.setTypeface(Typeface.MONOSPACE);

二、在Android中引入其他字体

第一步:在assets目录下新建fonts目录,把ttf字体文件放到此目录下。

第二步:程序中创建并设置字体。

//实例化TextView
TextView textView = findViewById(R.id.textview);

//得到AssetManager
AssetManager mgr=getAssets();

//根据路径得到Typeface
Typeface tf=Typeface.createFromAsset(mgr, "fonts/pocknum.ttf");

//设置字体
textView.setTypeface(tf);
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值