How to use Font Awesome icon in android application?

后端 未结 9 2114
时光说笑
时光说笑 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条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-02 21:48

    Similar code, But small change.

    Use AppCompatTextView, because when you using TextView, you will get a warning like this:

    This custom view should extend android.support.v7.widget.AppCompatTextView instead

    So kindly use AppCompatTextView. It will be better if you use AppCompatTextView instead of TextView:

    import android.graphics.Typeface;
    import android.support.v7.widget.AppCompatTextView;
    import android.content.Context;
    import android.util.AttributeSet;
    
    public class FontAwesome extends AppCompatTextView{
    
        public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
            super(context, attrs, defStyle);
            init();
        }
    
        public FontAwesome(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }
    
        public FontAwesome(Context context) {
            super(context);
            init();
        }
    
        private void init() {
    
            //Font name should not contain "/".
            Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                    "fontawesome.ttf");
            setTypeface(tf);
        }
    }
    

提交回复
热议问题