一、前言:
1、默认字体
Android SDK自带了四种字体:"normal"“monospace",“sans”, “serif”,
如下:

设置方式
1.通过XML文件设置
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="monospace"
android:textSize="20dp"
android:textColor="#000000"
android:typeface="monospace"
android:layout_margin="5dp"/>
2.Java代码中设置
TextView txtNormal = (TextView) findViewById(R.id.txt_normal);
txtNormal.setTypeface(Typeface.MONOSPACE);
二、设置第三方字体
1、Res中使用
右键选择项目的app / res文件夹,然后选择New > Android resource directory。

Resource type中选择font,File name名为font。

将字体文件拷贝到font中

java代码中使用
TextView txtNormal = (TextView) findViewById(R.id.txt_helvetica);
Typeface typeface = ResourcesCompat.getFont(this, R.font.helvetica);
txtNormal.setTypeface(typeface);
XML布局使用:
<TextView
android:id="@+id/tv_status"
android:includeFontPadding="false"
android:fontFamily="@font/helvetica"
android:layout_toLeftOf="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="正在举手"
/>
2、Assets中使用
新建Assets及fonts目录,并将字体文件拷贝到fonts目录下:

在java代码中使用
TextView txtNormal = (TextView) findViewById(R.id.txt_helvetica);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/helvetica.ttf");
txtNormal.setTypeface(typeface);
三、第三方框架全局字体设置
- 这里推荐一个第三方字体设置库Calligraphy,详细可以点击连接
网友评论