如何在安卓app中使用FontAwesome图标 参考:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0925/3518.html
根据原文来,但是要最新版本,并且修改了
下载
原文是4.7版本,现在使用5.4版本, 下载位置: https://fontawesome.com/?from=io
下载免费版,然后找到webfonts文件夹,找到下面所有ttf文件,没个文件后面重新命名,追加5.4.1版本号信息。然后放到fonts文件夹下面。
产生字典文件
打开: https://fontawesome.com/cheatsheet , 开始获得unicode,在控制台执行这段js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
var type; var doc; var articles; var allStr="<resources>\n"; type="solid"; doc=document.getElementById(type); articles=doc.getElementsByTagName("article"); for(var i=0; i<articles.length; i++){ var article=articles[i]; var id=article.id.split('-').join('_'); var unicode=article.getElementsByClassName("icon-unicode"); if(unicode!=null&&unicode.length>0){ unicode=unicode[0]; unicode=unicode.innerText; allStr=allStr+' <string name="fa_icon_'+type+"_"+id+'">&#x'+unicode+';</string>\n'; } } type="regular"; doc=document.getElementById(type); articles=doc.getElementsByTagName("article"); for(var i=0; i<articles.length; i++){ var article=articles[i]; var id=article.id.split('-').join('_'); var unicode=article.getElementsByClassName("icon-unicode"); if(unicode!=null&&unicode.length>0){ unicode=unicode[0]; unicode=unicode.innerText; allStr=allStr+' <string name="fa_icon_'+type+"_"+id+'">&#x'+unicode+';</string>\n'; } } type="brands"; doc=document.getElementById(type); articles=doc.getElementsByTagName("article"); for(var i=0; i<articles.length; i++){ var article=articles[i]; var id=article.id.split('-').join('_'); var unicode=article.getElementsByClassName("icon-unicode"); if(unicode!=null&&unicode.length>0){ unicode=unicode[0]; unicode=unicode.innerText; allStr=allStr+' <string name="fa_icon_'+type+"_"+id+'">&#x'+unicode+';</string>\n'; } } allStr=allStr+"</resources>"; console.log(allStr); |
放入: fontawesome_icons.xml
字体工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
package com.pandy.test.font; import android.app.Activity; import android.content.Context; import android.graphics.Typeface; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import java.util.ArrayList; import java.util.List; /** * Created by pandy on 18-10-25. */ public class FontManager { public static final String ROOT = "fonts/"; //public static final String FONTAWESOME = ROOT + "fontawesome-webfont-v470.ttf"; public static final List<String> fontNames=new ArrayList<String>(); static { //fontNames.add(FONTAWESOME); fontNames.add(ROOT + "fa-brands-400.5.4.1.ttf"); fontNames.add(ROOT + "fa-regular-400.5.4.1.ttf"); fontNames.add(ROOT + "fa-solid-900.5.4.1.ttf"); } /** * FontManager.markAsIconContainer(this,R.id.activity_login); * @param activity * @param id */ public static void markAsIconContainer(Activity activity,int id){ FontManager.markAsIconContainer(activity.getApplicationContext(), activity.findViewById(id)); } /** * FontManager.markAsIconContainer(getApplicationContext(),findViewById(R.id.activity_login)); * @param context * @param view */ public static void markAsIconContainer(Context context,View view){ for(String fontName: fontNames){ Typeface iconFont = FontManager.getTypeface(context, fontName); FontManager.markAsIconContainer(view, iconFont); } } private static Typeface getTypeface(Context context, String font) { return Typeface.createFromAsset(context.getAssets(), font); } private 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); } } } |
使用:layout.xml下面包含,这个layout必须给出id, 比如: R.id.activity_login
1 2 3 4 5 6 7 8 9 10 11 12 |
<TextView android:id="@+id/fa_icon_linechart" android:layout_width="100dp" android:layout_height="100dp" android:layout_alignStart="@+id/iv_icon" android:layout_below="@+id/btn_login" android:layout_marginTop="25dp" android:layout_weight="1" android:gravity="center" android:text="@string/fa_icon_solid_ad" android:textColor="@color/bootstrap_brand_primary" android:textSize="50dp" /> |
初始化替换:R.id.activity_login 这个必须是layout的id,不是R.layout.activity_login
1 2 3 4 5 6 7 8 9 10 11 12 |
public class Login1Activity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); TypefaceProvider.registerDefaultIconSets(); setContentView(R.layout.activity_login1); //FontManager.markAsIconContainer(getApplicationContext(),findViewById(R.id.activity_login)); FontManager.markAsIconContainer(this,R.id.activity_login); ButterKnife.bind(this); } } |
使用成功