[翻译]  How to use font icon (font-awesome) in XML selector

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


Is it possible to use font icon in selector instead of drawable ?

是否可以在选择器中使用字体图标而不是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 个解决方案

#1


4  

I changed text color in selector instead of drawable. Its working fine.

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

Create MyTextView class which extends TextView

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

Create text_color_selector.xml selector

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

And then use it in you layout

然后在你的布局中使用它

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

#2


1  

You can use font-awesome icons as follows :

您可以使用如下字体 - 真棒图标:

1 - Copy font-awesome font file to your assets directory

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

2 - Found the character entities for icons I wanted, using this page

2 - 使用此页面找到我想要的图标的字符实体

3 - Create entry in strings.xml for each icon. Eg:

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

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

4 - Load the font in onCreate method and set it for the appropriate Views:

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

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

Don't forget to reference the string in view.

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

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

check this link for more info.

查看此链接以获取更多信息。

you can't use it as selector. but you can dynamically change the icons.

你不能用它作为选择器。但您可以动态更改图标。

#3


1  

The easiest way!

最简单的方法!

Since Android Support Library 26 and Android Studio 3.0 you can use native fonts support in XML.

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

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

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

  1. Create font directory in res directory
  2. 在res目录中创建字体目录
  3. Copy font file into font directory
  4. 将字体文件复制到字体目录中
  5. 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.

    自14开始,使用android:font for API 26和app:font for support API。

  6. Now you can use fontFamily in TextView:

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

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

    要插入font-awesome char,请使用\ u输入UTF代码

NOTE: Android Studio design preview doesn't display font-awesome char but in application it works correctly.

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


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2018 ITdaan.com 粤ICP备14056181号