如何在XML选择器中使用字体图标(Font Awesome)

7

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

<?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>
3个回答

4

我在选择器中改变了文本颜色,而不是在可绘制对象中。它工作得很好。

创建一个继承自TextView的MyTextView类。

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>

3

最简单的方法!

自从 Android Support Library 26 和 Android Studio 3.0 推出以来,您可以在 XML 中使用本机字体支持。

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

  1. Create font directory in res directory
  2. Copy font file into font directory
  3. Create new Font resource file near font file

    <?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>
    

    Use android:font for API 26 and app:font for support API since 14.

  4. Now you can use fontFamily in TextView:

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:fontFamily="@font/font_awesome"
        android:text="\uF0da" />
    
  5. To insert font-awesome char enter it UTF code with \u

注意: Android Studio的设计预览不能显示字体图标,但在应用程序中可以正常工作。


2
您可以按照以下方式使用font-awesome图标:
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链接以获取更多信息。

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


谢谢您的回答,但是我想在选择器中使用,而不是简单的图标。 - mohsin raza
很遗憾,这是不可能的,因为它是字体(文本)。 - droidev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接