要自定义字体样式,首先你需要找到一个合适的.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)或者文件名不规范(最好小写英文)也会报这个异常的