转载请注明出处:http://blog.csdn.net/yyh352091626/article/details/53113677

作为一个Android开发者,自己想做一个app练手,有个比较头疼的问题就是没有UI图标资源~~ 其实很容易搞定的,下面就来聊聊如何在Android中应用图标字体库,找图标不再纠结!
Android引用ttf图标字体库_第1张图片

图标库传送门:https://icomoon.io/app/#/select

1、点击左上角菜单 -> Manager Projects 进入管理页面。 
Android引用ttf图标字体库_第2张图片

2、点击New Project, 创建一个工程,如First App并点击Load>
Android引用ttf图标字体库_第3张图片

3、点击Add Icon From Libray,去选择自己喜欢的Library,点击+Add添加到工程里面。Library有收费的,也有免费的,视情况而定。
Android引用ttf图标字体库_第4张图片

Android引用ttf图标字体库_第5张图片

4、转到资源页面。选择自己想要下载的图标,怎么都是灰色的?安啦,后面有惊喜! 
Android引用ttf图标字体库_第6张图片

5、点击右下角Generate Font,生成ttf字体库。 
Android引用ttf图标字体库_第7张图片

上面四个图标就是我前面选中的,下面的诸如e911文字就是图标对应的unicode符号,中文字体也是这么一个道理。点击download下载字体库。
Android引用ttf图标字体库_第8张图片

6、下载完成,解压。拷贝fonts/icomoon.ttf 到 android-assets/fonts 下面。
Android引用ttf图标字体库_第9张图片

7、应用字体。首先建一个字体“映射”类,反正官方不太推荐用枚举方式,暂且就用注解吧~~ 打开刚才解压包里面的demo.html,对应来创建字体映射。

[java]  view plain  copy
  1. import android.support.annotation.StringDef;  
  2.   
  3. /** 
  4.  * @author yuyh. 
  5.  * @date 2016/11/10. 
  6.  */  
  7. @StringDef({  
  8.         MDFonts.HOME,  
  9.         MDFonts.NEWSPAPER,  
  10.         MDFonts.MUSIC,  
  11.         MDFonts.PLAY  
  12. })  
  13. public @interface MDFonts {  
  14.   
  15.     String HOME = "\ue902";  
  16.   
  17.     String NEWSPAPER = "\ue904";  
  18.   
  19.     String MUSIC = "\ue911";  
  20.   
  21.     String PLAY = "\ue912";  
  22. }  
[java]  view plain  copy
  1. import android.content.Context;  
  2. import android.graphics.Typeface;  
  3. import android.widget.TextView;  
  4.   
  5. /** 
  6.  * @author yuyh. 
  7.  * @date 2016/11/10. 
  8.  */  
  9. public class MDFontsUtils {  
  10.   
  11.     public static Typeface OCTICONS;  
  12.   
  13.     /** 
  14.      * Get octicons typeface 
  15.      * 
  16.      * @param context 
  17.      * @return octicons typeface 
  18.      */  
  19.     public static Typeface getOcticons(final Context context) {  
  20.         if (OCTICONS == null)  
  21.             OCTICONS = getTypeface(context, "fonts/icomoon.ttf");  
  22.         return OCTICONS;  
  23.     }  
  24.   
  25.     /** 
  26.      * Set octicons typeface on given text view(s) 
  27.      * 
  28.      * @param textViews 
  29.      */  
  30.     public static void setOcticons(final TextView... textViews) {  
  31.         if (textViews == null || textViews.length == 0)  
  32.             return;  
  33.   
  34.         Typeface typeface = getOcticons(textViews[0].getContext());  
  35.         for (TextView textView : textViews)  
  36.             textView.setTypeface(typeface);  
  37.     }  
  38.   
  39.     /** 
  40.      * Get typeface with name 
  41.      * 
  42.      * @param context 
  43.      * @param name 
  44.      * @return typeface 
  45.      */  
  46.     public static Typeface getTypeface(final Context context, final String name) {  
  47.         return Typeface.createFromAsset(context.getAssets(), name);  
  48.     }  
  49. }  


9、图标对应是用TextView表示,而不是ImageView。如下:

[java]  view plain  copy
  1.       android:id="@+id/tvMusic"  
  2.       android:layout_width="wrap_content"  
  3.       android:layout_height="wrap_content"  
  4.       android:layout_margin="10dp"  
  5.       android:textSize="16dp" />  
  6.   
  7.   
  8.       android:id="@+id/tvHome"  
  9.       android:layout_width="wrap_content"  
  10.       android:layout_height="wrap_content"  
  11.       android:layout_margin="10dp"  
  12.       android:textSize="16dp" />  

在Java中应用字体。如下:

[java]  view plain  copy
  1. tvMusic = (TextView) findViewById(R.id.tvMusic);  
  2. tvMusic.setText(MDFonts.MUSIC);  
  3.   
  4. tvHome = (TextView) findViewById(R.id.tvHome);  
  5. tvHome.setText(MDFonts.HOME);  
  6.   
  7. // 应用字体  
  8. MDFontsUtils.setOcticons(tvMusic, tvHome);  

run起来,大功告成! 
Android引用ttf图标字体库_第10张图片

10、你会发现,run起来图标颜色全是Android默认的灰色,那么怎么更改图标颜色呢?刚才说了,图标字体用的是TextView,实际上他跟中文英文字体没什么两样,他本质上还是文字。所以,TextView怎么设置字体大小、字体颜色,这里就对应怎么设置。如下:

[java]  view plain  copy
  1. tvHome.setTextColor(Color.RED);  
  2. tvHome.setTextSize(50);  

   

Android引用ttf图标字体库_第11张图片

哈哈,没毛病!

当然,也可以把字体符号配置在string.xml

[java]  view plain  copy
  1. "icon_home" translatable="false">\ue902  
[java]  view plain  copy
  1.     android:id="@+id/tvHome"  
  2.     android:layout_width="wrap_content"  
  3.     android:layout_height="wrap_content"  
  4.     android:layout_margin="10dp"  
  5.     android:textSize="16dp"  
  6.     android:text="@string/icon_home" />  
   

// 当然,还需要下面这步来应用设置

[java]  view plain  copy
  1. MDFontsUtils.setOcticons(tvHome);  

更多用法大家就自行扩展吧!比如可以自定义一个TextView,直接应用字体,就不需要 MDFontsUtils.setOcticons(tvHome);  这步操作了,自己用就可以啦!

感谢阅读!

更多相关文章

  1. Android中字体颜色的设置
  2. Android 动态改变app图标
  3. Android中添加思源字体/NotoSansCJK/SourceHanSans
  4. android 9.0 10.0 修改默认字体大小
  5. (Android)react-native更改状态栏文字和图标颜色
  6. Android TextView中文字通过SpannableString来设置超链接、颜色
  7. Android根据包名取得指定程序包的信息(名称、图标……)
  8. Android应用程序中应用图标和名字的设置
  9. Android字体加粗

随机推荐

  1. Android xml资源文件animal动画解析
  2. opencv for android 编译
  3. Android 电源管理
  4. Android Bitmap
  5. Android 自定义ViewPager 实现轮播图
  6. Android Tablayout 的使用
  7. Android使用默认浏览器打开网页
  8. Android(安卓)Studio Error:Connection t
  9. android 5.0 新特性
  10. android五星评级