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

久未见 提交于 2019-12-22 05:41:38

问题


Is it possible to use font icon in selector instead of 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>

回答1:


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

Create MyTextView class which extends TextView

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

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


The easiest way!

Since Android Support Library 26 and Android Studio 3.0 you can use native fonts support in 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

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




回答3:


You can use font-awesome icons as follows :

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

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

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

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

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

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.



来源:https://stackoverflow.com/questions/35271608/how-to-use-font-icon-font-awesome-in-xml-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!