1、使用Typeface属性或自定义View修改字体
? 字体文件放在src\main\assets\fonts目录下;

可以使用方法直接修改字体:
tv.setTypeface(Typeface.createFromAsset(this.getAssets(), "fonts/ziti70.ttf"));
?也可以自定义View设置自定义字体:
public class Font70TextView extends AppCompatTextView {
public Font70TextView(@NonNull Context context) {
super(context);
init();
}
public Font70TextView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public Font70TextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
setTypeface(Typeface.createFromAsset(getContext().getAssets(), "fonts/ziti70.ttf"));
}
}
2、使用FontFamily属性可全局修改字体(Android8.0以后可用)

?字体文件放在src\main\res\font目录下;
全局设置字体:在style.xml中的Theme下新增:
<item name="android:fontFamily">@font/font50j</item>
?例:
<style name="Theme.WxDemo" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">@color/color_B5A36A</item>
<item name="colorPrimaryVariant">@color/color_B5A36A</item>
<item name="colorOnPrimary">@color/color_B5A36A</item>
<item name="android:fontFamily">@font/font50j</item>
</style>
或者单独给TextView设置字体包:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我试字体TextView"
android:fontFamily="@font/font70j"
/>
?
|