android 使用IconfontTextView

本文介绍了一种使用IconFontTextView的方法,通过将图标嵌入字体文件来替代ImageView,实现更小的APK体积、清晰的图片缩放效果及便捷的颜色调整。文章详细展示了如何创建自定义的IconFontTextView组件,并在布局文件中应用不同颜色的图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

IconFontTextView其实是图片做在字体文件(.ttf)中
每一个unicode码对应其下的一张图片
看下效果图吧:
这里写图片描述
如图中的图标即为IconFontTextView,并不是一个imageview
- 这样做可以缩小apk的体积
- 图片的放大、缩小不会发虚
- 易于更改颜色(只需更改textcolor)
- 如图实现图文混排

如何使用:

首先在Iconfont里将自己需要的图片加入购物车,下载代码,将压缩包解压后将后缀为ttf的文件拷贝到assets目录,打开**_unicode.html文件将没张图片对应的unicode码添加到string.xml里
这里写图片描述

IconfontTextView:

public class IconfontTextView extends android.support.v7.widget.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 iconfont = Typeface.createFromAsset(getResources().getAssets(), "iconfont.ttf");
        this.setTypeface(iconfont);
    }

}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.sign.iconfontdemo.MainActivity">

    <com.sign.iconfontdemo.IconfontTextView
        android:id="@+id/icon_text1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#f20b0b"
        android:textSize="16sp" />

    <com.sign.iconfontdemo.IconfontTextView
        android:id="@+id/icon_text2"
        android:layout_width="match_parent"
        android:textColor="#ed420e"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <com.sign.iconfontdemo.IconfontTextView
        android:id="@+id/icon_text3"
        android:layout_width="match_parent"
        android:textColor="#debb0d"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <com.sign.iconfontdemo.IconfontTextView
        android:id="@+id/icon_text4"
        android:textColor="#41cc17"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

    <com.sign.iconfontdemo.IconfontTextView
        android:id="@+id/icon_text5"
        android:textColor="#14739f"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp" />

</LinearLayout>
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IconfontTextView tvIcon1 = findViewById(R.id.icon_text1);
        IconfontTextView tvIcon2 = findViewById(R.id.icon_text2);
        IconfontTextView tvIcon3 = findViewById(R.id.icon_text3);
        IconfontTextView tvIcon4 = findViewById(R.id.icon_text4);
        IconfontTextView tvIcon5 = findViewById(R.id.icon_text5);
        tvIcon1.setText(R.string.smile);
        tvIcon2.setText(R.string.success);
        tvIcon3.setText(R.string.atm);
        tvIcon4.setText("广播" + getResources().getString(R.string.remind));
        tvIcon5.setText("比心" + getResources().getString(R.string.favorites));
    }
}

好啦,很简单吧。加油

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值