在android项目中使用FontAweSome图标集,可以方便的适配各种屏幕分辨率,不必在各种不同分辨率文件夹中新建资源文件。使用字体是一种很巧妙的方法,把图像以字体的方式呈现,这样以前设置为android:background的属性,现在只需要设置android:text就可一搞定,而且还可一适配不同的分辨率。

在XML文件中设置字体时,只需要指定android:typeface属性即可,但是 这个值只支持系统内置的几种字体。

导入FontAwesome字体,需要在项目中导入第三方字体。方式是在assets目录中新建一个fonts目录,然后将我们需要的ttf文件导入即可。以FontAwesome为例,git clone FontAwesome的地址为https://github.com/FortAwesome/Font-Awesome.git  ,下载之后在提取fonts目录下的fontawesome-webfont.ttf文件拷贝到fonts目录中。

建立一个帮助类:

 public class FontManager {
public static final String root = "fonts/";
public static final String FONTAWESOME = root + "fontawesome-webfont.ttf"; public static Typeface getTypeFace(Context context, String font) {
return Typeface.createFromAsset(context.getAssets(), font);
} }

布局文件:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/root"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"> <TextView
android:id="@+id/tv1"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:textColor="#00BFFF"
android:textSize="22sp"
android:typeface="monospace"
android:text="@string/ambulance"/> <TextView
android:id="@+id/tv2"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="22sp"
android:textColor="#87CEFA"
android:text="@string/bicycle"/> <TextView
android:id="@+id/tv3"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="22sp"
android:textColor="#00CED1"
android:text="@string/bus"/>
<TextView
android:id="@+id/tv4"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="22sp"
android:textColor="#CD5C5C"
android:text="@string/train"/>
</LinearLayout>
</RelativeLayout>

strings.xml:

 <resources>
<string name="app_name">FontAwesomeMotion</string>
<string name="ambulance"></string>
<string name="bus"></string>
<string name="bicycle"></string>
<string name="train"></string>
</resources>

在strings.xml的资源文件中,这样资源string,代表了不同的图标。可以通过 http://fortawesome.github.io/Font-Awesome/cheatsheet/  查询具体的图标。

 package com.app.motiongear.fontawesomemotion;

 import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView; import com.app.motiongear.utils.FontManager; import java.lang.reflect.Type; public class MainActivity extends AppCompatActivity { RelativeLayout mContainer; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Typeface tf = FontManager.getTypeFace(this, FontManager.FONTAWESOME);
mContainer = (RelativeLayout) this.findViewById(R.id.root);
for (int i = 0; i < mContainer.getChildCount(); i++) {
View currentView = mContainer.getChildAt(i);
if (currentView instanceof TextView) {
((TextView) currentView).setTypeface(tf);
}
} }
}

实际的显示效果如下:

最新文章

  1. 一些简单编程练习题P【持续更新】
  2. Swift 03.Dictionary
  3. 苹果IOS开发者账号的区别,企业账号,个人账号,公司团队账号,教育账号
  4. Java中时间日期格式化
  5. 树莓派安装kali后的简单配置
  6. ecshop 点购物车弹出提示框
  7. python中文字符串编码问题
  8. js验证身份证号码
  9. 使用NPOI读取Excel数据到DataTable
  10. HslCommunication组件库使用说明 (转载)
  11. 第88节:Java中的Ajax和ASP.NET和TCP/IP 教程和JSON
  12. [转]How to display the data read in DataReceived event handler of serialport
  13. jquery load 陷阱【原】
  14. STL的基本操作指令
  15. mysql undo 和redo 被误删除的恢复操作(一致性)
  16. android monitor 汉化 ddms
  17. web应用的乱码解决
  18. Ribbon 使用入门
  19. win7系统损坏无法进入系统(dsark.sys文件损坏)(未测试过)
  20. python3字符集之间--encode与decode之间的转码详解

热门文章

  1. layui加tp5图片上传实例
  2. Redis学习笔记(4)
  3. 前端基础进阶(十四):es6常用基础合集
  4. canvas锥形渐变进度条
  5. [书籍分享]0-001.rework(重来:更为简单有效的商业思维)
  6. pandas的loc与iloc
  7. 如何基于 echarts 在柱状图或条形图上实现转换率?(有想法吗?)
  8. 小谢第1问:为什么要写blog
  9. Java-语言基础梳理
  10. AUTOSAR-PDU&amp;SDU