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));
}
}
好啦,很简单吧。加油