Font Awesome to android

本文介绍如何在Android项目中集成FontAwesome字体图标,包括下载字体库、拷贝.ttf文件到项目目录、创建fontManager类管理和应用图标,以及在布局文件中使用图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

下载字体库   http://fontawesome.dashgame.com/

在fonts文件夹中找到fontawesome-webfont.ttf

--------4.7版本之后,好像就没有fonts文件夹了--------

将.ttf文件拷至我们的项目目录assets目录下 ,没有就新建

像这样:

 

 

用法:创建一个fontManager类,用于管理字体

public class FontManager {
    public static final String ROOT = "fonts/";
    public static final String FONT_AWESOME = ROOT + "fontawesome-webfont.ttf";

    public static Typeface getTypeface(Context context, String font) {
        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);
        }
    }

}

view中的text 可以去找一个自己需要显示的图标,https://fontawesome.com/v4.7.0/icons/#

点击自己想要的图标后,如下所示unicode:f2b9,在布局中填入text=“&#xf2b9”即可

 

<TextView
                android:id="@+id/fonticon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="&#xf2b9;"
                android:textColor="#44619d"
                android:textSize="50sp"
                android:onClick="facebook_onclick"/>

具体使用,传入view即可

FontManager.markAsIconContainer(findViewById(R.id.view), FontManager.getTypeface(this, FontManager.FONT_AWESOME));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值