Android IconFont

一、简介

Iconfont是阿里巴巴出版的矢量文字图标,目前仅支持单色的图标,优势在于体积小,使用方便,在Android中应用iconfont,可以减少安装包的体积。

二、使用方法

把iconfont.ttf文件放在main/assets/目录下,然后自定义TextView,识别字符编码

import android.content.Context;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;
import android.view.Gravity;

public class IconFontTextView extends AppCompatTextView {
    public IconFontTextView(Context context) {
        this(context, null);
    }

    public IconFontTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IconFontTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        Typeface typeface = Typeface.createFromAsset(getContext().getAssets(), "iconfont.ttf");
        this.setTypeface(typeface);
        this.setGravity(Gravity.CENTER);
    }
}
把图片的字符编码写入res/values/strings.xml中
    <string name="icon_fish">&#x343c;</string>
在布局文件中引用iconfont
            <com.IconFontTextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/icon_fish" />
代码错误写法:
mIconFontTextView.setText("&#x343c;");
代码正确写法:
mIconFontTextView.setText( getResources().getString(R.stringicon_fish));

三、系统自带Emoji

        int unicodeJoy = 0x1F602; -> 表情字符编码
        String emojiString = getEmojiStringByUnicode(unicodeJoy);
        mLoginButton.setText(emojiString);

        private String getEmojiStringByUnicode(int unicode){
            return new String(Character.toChars(unicode));
        }
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 1.IconFont概念 IconFont 通俗来说,就是用字体文件替代图片文件,来实现展示图片、图标效果。 优点...
    dean550阅读 581评论 0 0
  • mean to add the formatted="false" attribute?.[ 46% 47325/...
    ProZoom阅读 2,736评论 0 3
  • Android IconFont 动态设置 前言 现在iconfont使用的很普遍,一些小icon直接放在Text...
    javalong阅读 724评论 9 1
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,614评论 25 709
  • 每一个阶段,看不同的书。不要在适合的时间里看了本不属于这个阶段的书。细微的事情有时候就悄然改变了你的一生。都认为...
    我是懒懒懒懒懒懒人阅读 224评论 0 1