Android 阿里图标字体库
2021-09-26 本文已影响0人
坑逼的严
阿里图标库:https://www.iconfont.cn/
自己开发app时喜欢用的一个图库,经常在上面找一些图片,最近又发现了他一个不得了的功能,不喜勿喷,我也是才知道的。那就是把自己喜欢的图片收藏到自己的项目中,然后下载到本地,会有一个字体库,然后可以通过字体库像加载Text文字一样加载图标,大大的减少了项目中图标数量和占用大小了。
使用:
进入官网:
要登录,可以选择github方式,程序员都有的。随便找一个需要的图标

然后收藏他

添加到自己的项目中,没有的可以新建,记住自己用到的需要自己统一放到一个项目,不然后面会加载不出来。


添加完毕后会自动跳到你的项目栏下,这个时候我们点击下载到本地

下载完毕后就有字体库了。把它放入assets目录下

打开demo_index.html文件可以看到这一版本的字体库中,我们拥有的图标列表以及它对应的代码。然后就是封装textview了
自定义Textview
public class IconFontTextView extends AppCompatTextView {
public IconFontTextView(Context context) {
this(context, null);
}
public IconFontTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public IconFontTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs);
}
void initView(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.IconFontTextView);
String path = ta.getString(R.styleable.IconFontTextView_font_path);
ta.recycle();
Typeface tf;
if (TextUtils.isEmpty(path)) {
tf = Typeface.createFromAsset(getContext().getAssets(), "iconfont.ttf");
} else {
tf = Typeface.createFromAsset(getContext().getAssets(), path);
}
setTypeface(tf);
setIncludeFontPadding(false);
}
}
然后布局里面使用,android:textColor可以控制图标的颜色,这里设置为蓝色。
<com.example.demo.icon_font.IconFontTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/car"
android:textColor="@android:color/holo_blue_light"
android:gravity="center"
android:textSize="28sp"/>
效果图:

缺点
每次加个图标就要重新换一遍字体库,很累。