您的位置:首页 > 移动开发 > Android开发

android自定义图标字体的使用

2015-12-19 16:11 441 查看
字体图标库在网页中广泛应用,同样可以用在安卓上,优点就是无论在任何尺寸下,可缩放的矢量图形都会为您呈现出完美的图标。例如阿里的iconFoint库和Font Awesome的矢量图标,是我们常常用到的库。下面我们就在android 中自定义使用图标字体来替代ImageView。

步骤也很简单:

1. 下载字体库,例如fontawesome.ttf。放到android 项目中的assets目录下。也可以自己做一个图标字体库。

2. 在String.xml定义好图标对应的unicode编码(如下图)



3. 自定义TextView,代码很简单:

public class IconFontTextView extends TextView{
public IconFontTextView(Context context) {
super(context);
init(context);
}

public IconFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
/***
* 设置字体
*
* @return
*/
public void init(Context context) {
Typeface tf = Typeface.createFromAsset(context.getAssets(), "fontawesome.ttf");
setTypeface(tf);
}


在使用的时候:

<com.UI.IconFontTextView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="right"
android:text="@string/angle_right"/>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: