1.下载FontAweSome压缩包,取出FontAweSome-webfont.ttf添加至assets的fonts文件夹中
//注:①可根据自己的实际需要取ttf文件,具体需根据自己所需图标而定
②assets:在app文件夹下创建与java同等级的文件夹,在assets文件夹下创建fonts文件夹,将ttf文件放至此处
2.创建一个帮助类
public class FontManager{
public static final String Root="fonts/",
FONTAWESOME = Root+"FontAweSome-webfont.ttf";
public static Typeface getTypeface(Context context,String font){
//Typeface类指定typeface以及一个字体的特征,用于指明text在绘制(以及测量)的时候该如何显示
return Typeface.createFromAsset(context.getAssets(),font);
}
public static void markAsIconContainer(View v,Typeface typeface){
if(v instanceof ViewGroup){
ViewGroup vg = (ViewGroup) v;
for(int i=0;i<vg.getChildCount();i++){
View child = vg.getChildAt(i);
markAsIconContainer(child,typeface);
}
}else if(v instanceof TextView){
((TextView) v).setTypeface(typeface);
}
}
}
3.在onCreate方法中写下如下代码
Typeface iconFont = FontManager.getTypeface(getApplicationContext,FontManager.FONTAWESOME);
FontManager.marksAsIconContainer(findViewById(R.id.icons_container),iconFont);
4.在values文件夹下创建新文件icons.xml
<string name="fa_icon_areachart"></string>
5.在FontIconTextView中引用字符串
android:text="@string/fa_icon_areachart"
6.在build.gradle中添加引用
implementation 'com.shamanland:fonticon:0.1.8'