android iconfont xml,IconFont在Android中的使用介绍

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

iconfont介绍

常用iconfont字体库https://www.iconfont.cn/

http://fontawesome.dashgame.com/

https://ionicons.com/

优点:

1.IconFont是矢量图标,可以随意设置大小。

2.体积小可以减小APK的体积。

3.一套图标资源可以在不同平台使用(android ,ios,web)

4.很容易实现图文混排,因为都是Icon也被看做为文字.

缺点:

1.添加图标是需要重新制作ttf文件

2.只能支持单色(不支持渐变色图标)ttf文件中每个图片对应的一个unicode码。TextView设置文字的时候,使用对应的unicode码就能显示出图片。

具体使用

下载iconfont.ttf文件,放在项目main/assets/iconfont/下

方式一

xml中

android:id="@+id/tv1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/home"

android:textColor="@android:color/holo_orange_light"

android:textSize="50sp" />

strings中Unicode

最后直接设置Typeface iconfont1 = Typeface.createFromAsset(getAssets(), "iconfont/iconfont.ttf");

TextView textview1 = findViewById(R.id.tv1);

textview1.setTypeface(iconfont1);

方式二

自定义TextViewpublic class IconFontView extends AppCompatTextView {

private Context context;

private boolean isFirst = true;

private String text;

private int textColor;

private String clicked_text;

private int clicked_textColor;

public IconFontView(Context context) {

this(context, null);

}

public IconFontView(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public IconFontView(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

this.context = context;

init(attrs);

}

/** 初始化 */

private void init(AttributeSet attrs){

// 设置字体类型

setTypeface(Typeface.createFromAsset(context.getAssets(), "iconfont/iconfont.ttf"));

// 获取配置信息(text && textColor)

getAttrs(attrs);

}

/** 获得配置信息 */

private void getAttrs(AttributeSet attrs) {

TypedArray taCustom = context.obtainStyledAttributes(attrs, R.styleable.IconFontViewAttr);

clicked_text = taCustom.getString(R.styleable.IconFontViewAttr_clicked_text);

clicked_textColor = taCustom.getColor(R.styleable.IconFontViewAttr_clicked_textColor, 0);

taCustom.recycle();

}

@Override

public boolean onTouchEvent(MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_DOWN){

text = getText().toString();

textColor = getCurrentTextColor();

}

return super.onTouchEvent(event);

}

@Override

protected void dispatchSetPressed(boolean pressed) {

super.dispatchSetPressed(pressed);

if (isDuplicateParentStateEnabled()) {

if (pressed){

if(clicked_text != null)

this.setText(clicked_text);

if(clicked_textColor != 0)

this.setTextColor(clicked_textColor);

}else {

this.setText(text);

this.setTextColor(textColor);

}

}

}

}

在res/values/attrs中定义属性

使用

android:id="@+id/tv2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hot"

android:textColor="@android:color/holo_red_dark"

android:textSize="50sp" />

strings中Unicode

补充

可以图文混用我可以图文混用啦!!

使用阿里iconfont可以在demo_index.html文件中查找对应的Unicode

fontawesome查找Unicode对照表https://fontawesome.com/cheatsheet?from=io

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值