如何在 XML 选择器中使用字体图标 (font-awesome)

How to use font icon (font-awesome) in XML selector

是否可以在选择器中使用字体图标而不是可绘制图标?

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/menu_home_press" android:state_pressed="true"></item>
    <item android:drawable="@drawable/menu_home"></item>
</selector>

您可以使用字体超棒的图标,如下所示:

1 - 将 font-awesome 字体文件复制到您的资产目录

2 - 使用 this 页面

找到了我想要的图标的字符实体

3 - 在 strings.xml 中为每个图标创建条目。例如:

<string name="icon_eg">&#xf13d;</string>

4 - 在 onCreate 方法中加载字体并为适当的视图设置它:

Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );
...
Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);

不要忘记引用视图中的字符串。

<Button
     android:id="@+id/my_btn"
     style="?android:attr/buttonStyleSmall"
     ...
     android:text="@string/icon_eg" />

查看 this link 了解更多信息。

您不能将其用作选择器。但您可以动态更改图标。

我更改了选择器中的文本颜色而不是可绘制的。 它工作正常。

创建扩展 TextView

的 MyTextView class
public class MyTextView extends TextView {

    public MyTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public MyTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public MyTextView(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        Typeface tf = Typeface.createFromAsset(context.getAssets(),
                "fontawesome-webfont.ttf");
        setTypeface(tf);
    }
}

创建 text_color_selector.xml 选择器

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#ff0000" android:state_pressed="true" />
    <!-- pressed -->
    <item android:color="#ff0000" android:state_focused="true" />
    <!-- focused -->
    <item android:color="#000000" />
    <!-- default -->
</selector>

然后在你的布局中使用它

 <com.example.mohsin.myapplication.MyTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="50sp"
        android:clickable="true"
        android:textColor="@drawable/text_color_selector"
        android:text="\uF242">

    </com.example.mohsin.myapplication.MyTextView>

最简单的方法!

由于 Android 支持库 26 和 Android Studio 3.0,您可以在 XML 中使用本机字体支持。

https://developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html

  1. 在res目录下创建字体目录
  2. 复制字体文件到字体目录
  3. 在字体文件附近新建字体资源文件

    <?xml version="1.0" encoding="utf-8"?>
    <font-family xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
        <font
            android:font="@font/font_awesome_font"
            android:fontStyle="normal"
            android:fontWeight="400"
            app:font="@font/font_awesome_font"
            app:fontStyle="normal"
            app:fontWeight="400" />
    </font-family>
    

    使用 android:font 用于 API 26,使用 app:font 用于支持 API 自 14.

  4. 现在您可以在 TextView 中使用 fontFamily:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/font_awesome"
        android:text="\uF0da" />
    
  5. 要插入 font-awesome 字符,请输入带有 \u

  6. 的 UTF 代码

注意: Android Studio 设计预览不显示 font-awesome 字符,但在应用程序中它工作正常。