android中使用iconfont

来源:互联网 发布:窦靖童唱功知乎 编辑:程序博客网 时间:2024/06/06 05:55

项目缺少ui,但是又需要很多icon,为了和web的界面统一,android中,我也采用里iconfont的方案。写一个iconTextView继承TextView

package com.edusohoapp.app.view;import android.content.Context;import android.graphics.Typeface;import android.widget.TextView;/** * Created by suju on 14-5-12. */public class EduSohoIconView extends TextView{    private Context mContext;    public EduSohoIconView(Context context) {        super(context);        mContext = context;        initView();    }    public EduSohoIconView(android.content.Context context, android.util.AttributeSet attrs) {        super(context, attrs);        mContext = context;        initView();    }    private void initView()    {        Typeface iconfont = Typeface.createFromAsset(mContext.getAssets(), "normal.ttf");        setTypeface(iconfont);    }}

去网上选择一个齐全一点的iconfont库,然后下载ttf文件放到安卓的assets目录中。“normal.ttf”是我这里使用的字体名称。

下载一个fontlab studio 可以查看font字体的unicode编码,因为在安卓中TextView 要使用unicode值。




如上图,F002就是字体的unicode值。

这样更改TextView的text值为 就可以显示自定义的font

0 0