android自定义字体样式

要自定义字体样式,首先你需要找到一个合适的.ttf文件,在sdk里一有个这样的文件夹,路径为:  \sdk\platforms\android-N\data\fonts,当然你也可以去网上下载你需要的.

在xml自带四种,我感觉默认的normal和sans看上去没有什么区别,样式的改变对英文字体的支持比较好

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="sans"
    android:text="Hello World!" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="monospace"
    android:text="Hello World!" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="normal"
    android:text="Hello World!" />
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:typeface="serif"
    android:text="Hello World!" />

自定义一些别的字体样式可以在代码里设计,首先是建立assets/fonts这样的一个文件夹,然后将.ttf文件copy进去,最好将文件改纯英文的小写的格式,代码贴图如下:



关于assets的位置,我这里提一下,android studio切换到Project模式,依次点开app\src\main,点击main文件夹右键新建文件夹assets就OK了
关键代码就是:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/roboto.ttf");
textView.setTypeface(typeface);

需要注意的是:
fonts前面是不需要/的,如果写成"/fonts/roboto.ttf"是会报一个这样的异常的: java.lang.RuntimeException: native typeface cannot be made
我在网络上也找了一下别的资料,说的是ttf文件过大(大于5M)或者文件名不规范(最好小写英文)也会报这个异常的尴尬



 
 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值