美文网首页
TTF字体库系列文章1 —— Android使用ttf字体库替代

TTF字体库系列文章1 —— Android使用ttf字体库替代

作者: Amy_LuLu__ | 来源:发表于2018-09-17 15:52 被阅读0次

原文:Android使用ttf字体库替代替图片

我们知道Android中,有各种不同尺寸的屏幕,所以,就需要多套UI图片来进行手机的适配,这样,又会造成apk过大。
所以,使用字体文件来替换掉UI图片也属于APK瘦身的一种方式
本篇文章将介绍字体图标库的使用。

目录

  1. 获取ttf字体库
  2. 使用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">&#xe64c;</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);

结果展示:

相关文章

网友评论

      本文标题:TTF字体库系列文章1 —— Android使用ttf字体库替代

      本文链接:https://www.haomeiwen.com/subject/vqilnftx.html