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

标签 android xml font-awesome

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

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

最佳答案

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

创建扩展 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>

关于android - 如何在 XML 选择器中使用字体图标 (font-awesome),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35271608/

相关文章:

android - 为什么Android的一些权限在原来的Android Developers页面中没有列出?

css - 将 facebook 共享图标替换为超棒的 facebook 图标

angular - ionic 作用表 : How to use Custom Icons from Fontawesome?

.net - 解析来自 Web 服务的 XML 结果

java - 创建 Spring bean 时未知属性 accessExternalDTD

google-chrome - 选项组中断特殊字符

android - 如何更改主题 "Theme.AppCompat.Light.NoActionBar"的选项菜单点颜色

android - 有什么方法可以通过服务务实地唤醒屏幕吗?

java - 从 FTP 服务器读取文件,但 InputStream 始终为空

java - 从 xml 格式的响应中读取数据