Font Awesome to android

下载字体库   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));

猜你喜欢

转载自blog.csdn.net/Super_Jadroid/article/details/84313643