如何在Native Android Application上使用"Font Awesome"中的图标和符号

Jul*_*rez 152 android font-awesome

我正在尝试在我的应用程序上使用Font Awesome,我能够使用该字体集成Typeface.createFromAsset(),但我也想使用这种字体提供的图标,但到目前为止我还没能做到这一点.

此特定字体包含Unicode专用区(PUA)内的图标,用于媒体播放器控件,文件系统访问,箭头等内容.

有没有人在Android上使用包含图标和符号的字体,这有可能吗?

小智 305

在我的Android应用程序中,Font Awesome似乎对我很好.我做了以下事情:

  1. 复制fontawesome-webfont.ttf到我的assests文件夹中
  2. 使用此页面找到我想要的图标的角色实体:http://fortawesome.github.io/Font-Awesome/cheatsheet/
  3. 在strings.xml中为每个图标创建了一个条目.例如,一颗心:

    <string name="icon_heart">&#xf004;</string>
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在我的xml布局视图中引用了所述条目:

     <Button
         android:id="@+id/like"
         style="?android:attr/buttonStyleSmall"
         ...
         android:text="@string/icon_heart" />
    
    Run Code Online (Sandbox Code Playgroud)
  5. 在我的onCreate方法中加载字体并将其设置为适当的视图:

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

  • 很好的主意.但是,在为每个图标不断创建新字体时要小心内存问题.相反,使用"Hashtable"之类的东西重新使用您的图标字体,如下所示:https://code.google.com/p/android/issues/detail?id = 9994#c7 (46认同)
  • 我为此创建了一个项目:https://bitbucket.org/informatic0re/awesome-font-iconview (4认同)
  • 将动态字符串添加到textview,如text.setText(Html.fromHtml("&#xf000;")); (2认同)

chi*_*uki 28

试试IcoMoon:http://icomoon.io

  • 选择你想要的图标
  • 为每个图标指定字符
  • 下载字体

比如说,您选择了播放图标,为其分配了字母"P",并将文件下载icomoon.ttf到资产文件夹中.这是你显示图标的方式:

XML:

<TextView
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:textSize="48sp"
  android:text="P" />
Run Code Online (Sandbox Code Playgroud)

Java的:

Typeface typeface = Typeface.createFromAsset(getAssets(), "icomoon.ttf");
textView.setTypeface(typeface);
Run Code Online (Sandbox Code Playgroud)

我已经谈过制作精美的Android应用程序,其中包括使用图标字体的说明,以及添加渐变以使图标更漂亮:http: //www.sqisland.com/talks/beautiful-android

图标字体说明从幻灯片34开始:http: //www.sqisland.com/talks/beautiful-android/#34


lil*_*tof 10

也许为时已晚,但我有同样的需求,所以我发布了这个https://github.com/liltof/font-awsome-for-android 这是一个Android准备好的xml版本的字体真棒可用就像Keith Corwin说的

希望它会帮助别人.


use*_*907 8

以上是很好的例子,效果很好:

Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf" );

Button button = (Button)findViewById( R.id.like );
button.setTypeface(font);
Run Code Online (Sandbox Code Playgroud)

但!>如果从xml设置的按钮内的字符串,这将有效:

<string name="icon_heart">&#xf004;</string>
button.setText(getString(R.string.icon_heart));
Run Code Online (Sandbox Code Playgroud)

如果需要动态添加它可以使用:

String iconHeart = "&#xf004;";
String valHexStr = iconHeart.replace("&#x", "").replace(";", "");
long valLong = Long.parseLong(valHexStr,16);
button.setText((char) valLong + "");
Run Code Online (Sandbox Code Playgroud)


Ole*_* K. 5

有一个小而有用的库是为此目的而设计的

dependencies {
    compile 'com.shamanland:fonticon:0.1.9'
}
Run Code Online (Sandbox Code Playgroud)

在Google Play上获取演示。

在此输入图像描述

您可以轻松地在布局中添加基于字体的图标:

<com.shamanland.fonticon.FontIconView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/ic_android"
    android:textSize="@dimen/icon_size"
    android:textColor="@color/icon_color"
    />
Run Code Online (Sandbox Code Playgroud)

您可以Drawable从 xml 中扩充 font-icon:

<?xml version="1.0" encoding="utf-8"?>
<font-icon
    xmlns:android="http://schemas.android.com/apk/res-auto"
    android:text="@string/ic_android"
    android:textSize="@dimen/big_icon_size"
    android:textColor="@color/green_170"
    />
Run Code Online (Sandbox Code Playgroud)

Java代码:

Drawable icon = FontIconDrawable.inflate(getResources(), R.xml.ic_android);
Run Code Online (Sandbox Code Playgroud)

链接: