我们知道Android中,有各种不同尺寸的屏幕,所以,就需要多套UI图片来进行手机的适配,这样,又会造成apk过大。
所以,使用字体文件来替换掉UI图片也属于APK瘦身的一种方式。
本篇文章将介绍字体图标库的使用。
目录
- 获取ttf字体库
- 使用ttf字体库
1.获取ttf字体库
阿里巴巴提供了一个图标库Iconfont,我们可以去这里下载自己需要图标。
当然也可以找UI做。
① 选中图标加入购物车

② 下载文件

③ 获取ttf字体库

iconfont.ttf 图标生成的字体文件
demo_unicode文件,可以看到里面图标unicode编码

2.使用ttf字体库
①把 iconfont.ttf 字体库 放到 assets目录 下

②打开里面 demo_unicode文件,可以看到里面图标 unicode编码

③ 我们在values下面的string.xml中创建我们字体图标
<resources>
<string name="icon_repair"></string>
</resources>
现在,我们就能在项目中,使用我们的字体图标了,首先创建TextView
<TextView
android:id="@+id/tv_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:textColor="@color/colorAccent"
android:text="@string/icon_repair"/>
④ 在代码中为TextView设置字体文件
// 加载字体文件
Typeface typeface = Typeface.createFromAsset(getAssets(),"iconfont.ttf");
TextView mTxtView = (TextView) findViewById(R.id.textview);
// 为TextView设置字体文件
mTxtView.setTypeface(typeface);
结果展示:

网友评论