上一篇把APP的远程配置与启动开发完了。
本篇讲述iconfont字体图标在Android中的使用。
直接进入操作步骤:
1、自定义IconView
IconView继承TextView。
public class IconView extends AppCompatTextView {
public IconView(Context context) {
this(context,null);
}
public IconView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public IconView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
void init(Context context){
//初始化iconfont字体文件的路径。
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"icons/iconfont.ttf"));
}
}
这里的icons/iconfont.ttf
目录如下:
2、下载iconfont.ttf文件
在iconfont.cn上找到合适的icon,点击添加入库(购物车的按钮)。
选好后,点击浏览器右上角的购物车按钮。
然后点击“下载代码”,下载完成后解压。
iconfont.ttf为Android中所需文件。
3、使用图标
在Android工程的布局文件里使用IconView:
<com.kanhui.laowulao.widget.IconView
android:id="@+id/iv_light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textColor="@color/main_green"
android:textSize="@dimen/font_50"
android:layout_centerHorizontal="true"
android:layout_margin="@dimen/dimen_20"
/>
其中android:text
属性的值来自于iconfont文件。
找到第二步中下载的解压文件中的demo_index.html
。
用浏览器打开:
把图标下面的&#
开头的这一串拷贝到android:text
的值里面即可。
项目是个循序渐进的过程。后续需要添加新的icon,直接在iconfont.cn继续上面的操作,把新下载的iconfont.ttf文件替换原来文件就能生效。
使用iconfont很方便,小巧灵活,不用考虑图片大小,不会失真,颜色自由调整等等。
一站式开发一个安卓APP系列,开发篇结束了,共6篇。没什么技术难点,都是现成的技术,只是个人用来快速做一个完整的APP,然后觉得这个过程可以记录下来,仅此而已。其中有一些代码也是复用的网上现成的,如果有侵权问题,会立即删掉。