如何在Android应用程序中使用Font Awesome图标? [英] How to use Font Awesome icon in android application?

查看:106
本文介绍了如何在Android应用程序中使用Font Awesome图标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Android应用程序中使用Font Awesome的图标集.我有一些TextView来设置这些图标.我不想使用任何PNG图片.我的Textview就是这样->

<TextView
    android:id="@+id/userLogin"
    android:text="Login Now"
    android:clickable="true"
    android:onClick="login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

否,我想在立即登录文本之前放置一个图标.该怎么做?

解决方案

您可以按照以下此处下载fontawesome.ttf.并将文件放在 asset/fontawesome.ttf 中.

然后创建一个FontAwesome类,它实际上以这种方式表示FontAwesome的文本视图.

public class FontAwesome extends TextView {


    public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public FontAwesome(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FontAwesome(Context context) {
        super(context);
        init();
    }

    private void init() {

    //Font name should not contain "/".
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fontawesome.ttf");
        setTypeface(tf);
    }

}

现在您可以根据需要使用Fontawesome类,并按照 cheatsheet.来获取图标的Unicode.

因此,您的文本视图将是这样.

<PACKAGE_NAME.Fontawesome
    android:id="@+id/userLogin"
    android:text="&#xf007;  Login Now"
    android:clickable="true"
    android:onClick="login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

I want to use Font Awesome's icon set in my android application. I have some TextView to set those icons. I don't want to use any png image. My Textview is like this ->

<TextView
    android:id="@+id/userLogin"
    android:text="Login Now"
    android:clickable="true"
    android:onClick="login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

No, I want to put a icon before the text Login Now. How to do that ?

解决方案

You can follow this answer.

First Download the fontawesome.ttf from here. And put the file in asset/fontawesome.ttf .

Then Make a FontAwesome class which actually represent the textview of FontAwesome like this way.

public class FontAwesome extends TextView {


    public FontAwesome(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public FontAwesome(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FontAwesome(Context context) {
        super(context);
        init();
    }

    private void init() {

    //Font name should not contain "/".
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                "fontawesome.ttf");
        setTypeface(tf);
    }

}

now you can use the Fontawesome class as your need and also follow the cheatsheet. to get your icon's Unicode.

So, your textview will be like this .

<PACKAGE_NAME.Fontawesome
    android:id="@+id/userLogin"
    android:text="&#xf007;  Login Now"
    android:clickable="true"
    android:onClick="login"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

这篇关于如何在Android应用程序中使用Font Awesome图标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆