IconFont在Android中的使用

2016-04-23  本文已影响5893人  cocolove2

什么是iconFont


iconFont优缺点

1.由于IconFont是矢量图标,所以可以轻松解决图标适配问题
2.图标以字体文件的形式存在项目中(.ttf文件一般放在assets文件夹下),体积小可以减小APK的体积。
3.一套图标资源可以在不同平台使用(android ,ios,web)
4.资源维护方便。
1.需要自定义svg图片,并将其转换为ttf文件,图标制作成本比较高
2.添加图标是需要重新制作ttf文件
3.只能支持单色(不支持渐变色图标)

IconFont在android端的使用

终于来到正文啦,那我们就看看iconfont如何使用吧!

iconfont图标
//我这里的图标文件是iconfont.ttf(目录:assets/fonts/iconfont.ttf)
TextView tv1=(TextView)findViewById(R.id.xxxx);
Typeface    typeface=Typeface.createFromAsset(getAssets(),
"fonts/iconfont.ttf");
tv1.setTypeface(typeface);
//设置图标(对应上面的点赞图标)(Unicode编码)
tv1.setText("\ue600");

效果图

iconfont-screen.gif

Demo代码片段

<TextView     
 android:id="@+id/main_text"  
  android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:clickable="true"    
android:textColor="@color/text_color_selector"     />
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/darker_gray" android:state_pressed="true"/> 
   <item android:color="@android:color/holo_blue_bright"/>
</selector>

github上也有IconFont支持库

                            觉得不错点个赞呗~哈哈

参考文章

浅谈矢量图形前景
知乎回答IconFont
Icon Font --Android图标也能轻松换肤
如何在移动设备上应用iconfont

上一篇 下一篇

猜你喜欢

热点阅读