问题描述
我想在我的 android 应用程序中使用 Font Awesome 的图标集.我有一些 TextView
来设置这些图标.我不想使用任何 png 图像.我的Textview是这样的->
I want to use Font Awesome's icon set in my android application. I have some TextView
to set those icons. I don't want to use any png image. My Textview is like this ->
<TextView
android:id="@+id/userLogin"
android:text="Login Now"
android:clickable="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
不,我想在立即登录"文本之前放置一个图标.该怎么做?
No, I want to put a icon before the text Login Now. How to do that ?
推荐答案
你可以关注这个答案.
首先从这里下载fontawesome.ttf.并将文件放入 asset/fontawesome.ttf.
First Download the fontawesome.ttf from here. And put the file in asset/fontawesome.ttf.
然后制作一个 FontAwesome
类,像这样实际代表 FontAwesome
的文本视图.
Then Make a FontAwesome
class which actually represents the textview of FontAwesome
like this way.
public class FontAwesome extends TextView {
public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public FontAwesome(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public FontAwesome(Context context) {
super(context);
init();
}
private void init() {
//Font name should not contain "/".
Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
"fontawesome.ttf");
setTypeface(tf);
}
}
现在您可以根据需要使用 Fontawesome 类,还可以按照 cheatsheet. 获取图标的 Unicode.
now you can use the Fontawesome class as your need and also follow the cheatsheet. to get your icon's Unicode.
所以,你的 TextView 会是这样的.
So, your TextView will be like this.
<PACKAGE_NAME.FontAwesome
android:id="@+id/userLogin"
android:text=" Login Now"
android:clickable="true"
android:onClick="login"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
这篇关于如何在 Android 应用程序中使用 Font Awesome 图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!