下载 Font Awesome :http://www.fontawesome.com.cn/
下载完毕直接解压,找到 fonts/fontawesome-webfont.ttf ;
放在项目 assets 目录下:
打开下面的网址,找到你想要的图标:http://www.fontawesome.com.cn/cheatsheet/
编写 string 文档:
<string name="homePageIcon"></string>
在xml文件中加入下面代码:
<TextView
android:id="@id/tv_main_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/homePageIcon"
android:textColor="@color/blue_e2"
android:textSize="@dimen/qb_px_40"/>
最后在代码中添加:
public Typeface font;
font = Typeface.createFromAsset(getAssets(),"fonts/fontawesome-webfont.ttf");
tv_main_icon.setTypeface(font);
运行就可以了。
到这里结束了。
===================================================================================
下面是另外一个获取图标的方法,供参考。
FontAwesomeIconView 是 Android 的小部件,用于显示常用的图标;
下面就来介绍一下简单的使用方法,达到显示下面图片的效果。
首先添加 gradle :
implementation 'com.shifz:font-awesome-icon-view:0.0.1'
然后在 xml 文件中添加如下面的代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<com.shifz.fontawesomeiconview.FAIconView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:iconColor="#E60012"
custom:iconName="@string/fa_trash"
custom:iconSize="12sp" />
</LinearLayout>
这里要注意:
xmlns:custom="http://schemas.android.com/apk/res-auto"
上面这行代码,不要添加错了,添加错了会导致不显示。
设置图片颜色:custom:iconColor="#E60012"
设置图片名称(通过设置不同的名称来显示不同的图片):custom:iconName="@string/fa_trash"
设置图片大小:custom:iconSize="12sp"
运行就可以看到效果了~
我们可以根据下面的网站,看到图片效果图和名称(如果加载慢,请刷新重试):
https://fontawesome.com/cheatsheet?from=io
下面是官方 github 给出的例子:
https://github.com/theapache64/FontAwesomeIconView