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

时间:2016-02-08 14:07:09

标签: android xml font-awesome

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

<?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 个答案:

答案 0 :(得分:4)

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

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

答案 1 :(得分:2)

最简单的方法!

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

    自14开始,使用android:font用于API 26和app:font用于支持API。

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

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

  6. 的UTF代码

    注意: Android Studio设计预览不显示字体真棒字符,但在应用程序中它可以正常工作。

答案 2 :(得分:1)

您可以按如下方式使用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链接了解详情。

你不能将它用作选择器。但你可以动态改变图标。

相关问题