android使用FontAweSome图标

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">&#xf1fe;</string>

5.在FontIconTextView中引用字符串

android:text="@string/fa_icon_areachart"

6.在build.gradle中添加引用

implementation 'com.shamanland:fonticon:0.1.8'

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值