<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">由于每次到网上找图片太麻烦,想在Android项目上调用web上的font-awesome字体库做为图标,所以花时间研究了一下。其实原理很简单,同web一样的原理调用font-awesome的字体库,只不过图标字符串需要转换一下。</span>
首先到font-awesome官网下载font-awesome.ttf字体,将字体放在assets目录下。然后我将TextView封装了一下,以便直接调用显示图标文字。
public class IconText extends TextView{
private Typeface font;
public IconText(Context context) {
super(context);
font = Typeface.createFromAsset(context.getAssets(),
"fontawesome.ttf");
this.setTypeface(font);
}
public IconText(Context context,AttributeSet attr){
super(context,attr);
font = Typeface.createFromAsset(context.getAssets(),
"fontawesome.ttf");
this.setTypeface(font);
}
public IconText(Context context,AttributeSet attr,int defStyle)
{
super(context,attr,defStyle);
font = Typeface.createFromAsset(context.getAssets(),
"fontawesome.ttf");
this.setTypeface(font);
}
}
使用时:
<com.scum.ui.IconText
android:layout_width="50dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/icon_key"
android:textColor="#0080FF"
android:textSize="20sp" />
至于图标字符串的转换,请下载xml。
点击下载xml
比如icon-key这个图标,对应的字符串为xml中的
<string name="fa_key"></string>
使用font-awesome库后,减少了找图P图的时间,作为简单的图标标识还是很方便的。