一、前言:
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中使用字体
显示如图:
- 在res下创建font包,里面加入字体类型,就可以直接引用
java代码中使用
TextView txtNormal = (TextView) findViewById(R.id.txt_normal);
Typeface typeface = ResourcesCompat.getFont(this, R.font.bold);
txtNormal.setTypeface(typeface);
XML布局使用:
<TextView
android:id="@+id/tv_status"
android:includeFontPadding="false"
android:fontFamily="@font/bold"
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,详细可以点击连接