第二步:打开 iconfont 目录中的 demo.html,找到图标相对应的 HTML 实体字符码;
第三步:打开 res/values/strings.xml,添加 string 值;
<string name="icons"> 手机</string>第四步:打开 activity_main.xml,添加 string 值到 TextView:
<TextViewandroid:id="@+id/like"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/icons" />第五步:为 TextView 指定文字:
import android.graphics.Typeface;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Typeface iconfont = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");TextView textview = (TextView)findViewById(R.id.like);textview.setTypeface(iconfont);}设置完效果如下
就是这么简单完事。但是我们发现在activity
代码中setTypeface
很没有必要。因为我们整个应用有很多页面都需要设置字体图标时,这样设置会有很多垃圾代码产生。这时我们可以用一个简单的自定义view
就解决问题
public class IconFontTextview extends TextView {public IconFontTextview(Context context) {super(context); init(context);}public IconFontTextview(Context context, AttributeSet attrs) { super(context, attrs);init(context);}public IconFontTextview(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context);} private void init(Context context){Typeface iconfont = Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"); setTypeface(iconfont);}}然后就是改一下我们布局文件
<com.xiaoming.liaoliao.view.IconFontTextview android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="20dp"android:textColor="@android:color/holo_red_dark"android:text=" 手机" />其他
textview
的属性还是正常使用,解决