How to use Font Awesome icon in android application?

后端 未结 9 2113
时光说笑
时光说笑 2020-12-02 20:58

I want to use Font Awesome\'s icon set in my android application. I have some TextView to set those icons. I don\'t want to use any png image. My Textview is li

9条回答
  •  猫巷女王i
    2020-12-02 21:36

    Here are the steps to follow:

    1. First download font-awesome from here: http://fontawesome.io/
    2. Create assets folder and add fontawesome-webfont.ttf to this folder
    3. Create a helper class by below code:
    public class FontManager {
        public static final String ROOT = "fonts/",
        FONTAWESOME = ROOT + "fontawesome-webfont.ttf";   
        public static Typeface getTypeface(Context context, String font) {
            return Typeface.createFromAsset(context.getAssets(), font);
        }    
    }
    

    4. Now use font awesome to your textview using below code

    Typeface iconFont = FontManager.getTypeface(getApplicationContext(), FontManager.FONTAWESOME);
    tvIcon1 = (TextView) findViewById(R.id.tvIcon1);
    tvIcon2 = (TextView) findViewById(R.id.tvIcon2);
    tvIcon3 = (TextView) findViewById(R.id.tvIcon3);
    tvIcon1.setTypeface(iconFont);
    tvIcon2.setTypeface(iconFont);
    tvIcon3.setTypeface(iconFont);
    

    You can get full source code in my blog post here.

提交回复
热议问题