【问题标题】:How to use icons and symbols from "Font Awesome" on Native Android Application如何在原生 Android 应用程序上使用“Font Awesome”中的图标和符号
【发布时间】:2013-02-19 01:43:23
【问题描述】:

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

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

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

【问题讨论】:

标签: android font-awesome


【解决方案1】:

Font Awesome 似乎在我的 Android 应用中运行良好。我做了以下事情:

  1. 已将 fontawesome-webfont.ttf 复制到我的资产文件夹中
  2. 找到我想要的图标的字符实体,使用这个页面:http://fortawesome.github.io/Font-Awesome/cheatsheet/
  3. 在 strings.xml 中为每个图标创建了一个条目。例如心脏:

    <string name="icon_heart">&#xf004;</string>
    
  4. 在我的 xml 布局视图中引用了上述条目:

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

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

【讨论】:

  • 好主意。但是在为每个图标不断创建新字体时要注意内存问题。相反,使用Hashtable 之类的东西来重新使用您的图标字体,就像这里建议的那样:code.google.com/p/android/issues/detail?id=9904#c7
  • 我为此创建了一个项目:bitbucket.org/informatic0re/awesome-font-iconview
  • 我的代码有一些问题:如果我在strings.xml 中声明字符串,如&lt;string name="faicon"&gt;&amp;#xf001;&lt;/string&gt;,然后在我的代码中设置文本,就可以了。但是当我尝试mTextView.setText("&amp;#xf004;"); 时,它只显示原始文本。任何人都可以帮忙吗?谢谢
  • 像这样向文本视图添加动态字符串 text.setText(Html.fromHtml(""));
【解决方案2】:

试试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" />

java:

Typeface typeface = Typeface.createFromAsset(getAssets(), "icomoon.ttf");
textView.setTypeface(typeface);

我发表了关于制作精美 Android 应用程序的演讲,其中包括对使用图标字体的说明,以及添加渐变以使图标更漂亮: http://www.sqisland.com/talks/beautiful-android

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

【讨论】:

    【解决方案3】:

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

    希望它能帮助其他人。

    【讨论】:

    • 欢迎来到 SO。我看到你的代码不是太长。你为什么不把它贴在这个答案上?链接可能会随着时间的推移而崩溃。
    • 我已经使用这个很棒的 .xml 文件好几个星期了。但现在我意识到它错过了 fa_times。你能更新一下吗?编辑:哦,它是 fa_remove。或 icon_remove 在您的文件中。
    • 如何使用真棒字体在按钮中添加文本和图标?就像在按钮中添加可绘制的左侧或可绘制的右侧。
    【解决方案4】:

    上面是一个很好的例子,效果很好:

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

    但是! > 如果您从 xml 设置按钮内的字符串,这将起作用:

    <string name="icon_heart">&#xf004;</string>
    button.setText(getString(R.string.icon_heart));
    

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

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

    【讨论】:

    • 有史以来最好的方法!谢谢我给+投票_只是注意按钮是textview并用get string替换String.valueOf
    • 终于找到了一个使用 font awesome 的解决方案。非常感谢!
    【解决方案5】:

    如果您想要编程 setText 而不向 string.xml 添加字符串

    在此处查看其十六进制代码:

    http://fortawesome.github.io/Font-Awesome/cheatsheet/

    替换 到 0xf066

     Typeface typeface = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
        textView.setTypeface(typeface);
        textView.setText(new String(new char[]{0xf006 }));
    

    【讨论】:

      【解决方案6】:

      有小实用library designed for this purposes

      dependencies {
          compile 'com.shamanland:fonticon:0.1.9'
      }
      

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

      您可以从 xml 将 font-icon 膨胀为 Drawable

      <?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"
          />
      

      Java 代码:

      Drawable icon = FontIconDrawable.inflate(getResources(), R.xml.ic_android);
      

      链接:

      【讨论】:

      • 非常好的图书馆,谢谢。充气的 xml 定义虽然有点痛苦,但我更喜欢直接使用 FontIconView
      • 我在一台装有 Android 4.2.2 的 Lenovo 中测试过,但图标不显示,可能是什么?
      • 试用最新版本0.1.9
      【解决方案7】:

      我在 C# (Xamarin) 中创建了这个帮助程序类,以编程方式设置文本属性。它对我来说效果很好:

      internal static class FontAwesomeManager
      {
          private static readonly Typeface AwesomeFont = Typeface.CreateFromAsset(App.Application.Context.Assets, "FontAwesome.ttf");
      
          private static readonly Dictionary<FontAwesomeIcon, string> IconMap = new Dictionary<FontAwesomeIcon, string>
          {
              {FontAwesomeIcon.Bars, "\uf0c9"},
              {FontAwesomeIcon.Calendar, "\uf073"},
              {FontAwesomeIcon.Child, "\uf1ae"},
              {FontAwesomeIcon.Cog, "\uf013"},
              {FontAwesomeIcon.Eye, "\uf06e"},
              {FontAwesomeIcon.Filter, "\uf0b0"},
              {FontAwesomeIcon.Link, "\uf0c1"},
              {FontAwesomeIcon.ListOrderedList, "\uf0cb"},
              {FontAwesomeIcon.PencilSquareOutline, "\uf044"},
              {FontAwesomeIcon.Picture, "\uf03e"},
              {FontAwesomeIcon.PlayCircleOutline, "\uf01d"},
              {FontAwesomeIcon.SignOut, "\uf08b"},
              {FontAwesomeIcon.Sliders, "\uf1de"}
          };
      
          public static void Awesomify(this TextView view, FontAwesomeIcon icon)
          {
              var iconString = IconMap[icon];
      
              view.Text = iconString;
              view.SetTypeface(AwesomeFont, TypefaceStyle.Normal);
          }
      }
      
      enum FontAwesomeIcon
      {
          Bars,
          Calendar,
          Child,
          Cog,
          Eye,
          Filter,
          Link,
          ListOrderedList,
          PencilSquareOutline,
          Picture,
          PlayCircleOutline,
          SignOut,
          Sliders
      }
      

      我认为应该很容易转换为 Java。希望它可以帮助某人!

      【讨论】:

        【解决方案8】:

        我用于 Font Awesome 的库之一是:

        https://github.com/Bearded-Hen/Android-Bootstrap

        具体来说,

        https://github.com/Bearded-Hen/Android-Bootstrap/wiki/Font-Awesome-Text

        文档很容易理解。

        首先,在build.gradle中添加需要的依赖:

        dependencies {
            compile 'com.beardedhen:androidbootstrap:1.2.3'
        }
        

        其次,您可以将其添加到您的 XML 中:

        <com.beardedhen.androidbootstrap.FontAwesomeText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            fontawesometext:fa_icon="fa-github"
            android:layout_margin="10dp" 
            android:textSize="32sp"
        />
        

        但如果您想使用上面的代码示例,请确保将其添加到您的根布局中:

            xmlns:fontawesometext="http://schemas.android.com/apk/res-auto"
        

        【讨论】:

        • 我遵循了这个但我得到了错误:错误:(54)在包'xxx.yyy'中找不到属性'fa_icon'的资源标识符
        • @Hajjat​​ 要使用它,您需要使用 1.2.3 版。我刚刚更新了代码以反映这一点。试试看。
        【解决方案9】:

        FontView 库可让您在应用中使用普通/unicode 字体字符作为图标/图形。它可以通过资产或网络位置加载字体。

        这个库的好处是:

        1 - it takes care of remote resources for you
        2 - scales the font size in dynamically sized views
        3 - allows the font to easily be styled.
        

        https://github.com/shellum/fontView

        例子:

        Layout:

        <com.finalhack.fontview.FontView
                android:id="@+id/someActionIcon"
                android:layout_width="80dp"
                android:layout_height="80dp" />
        

        Java:

        fontView.setupFont("fonts/font.ttf", character, FontView.ImageType.CIRCLE);
        fontView.addForegroundColor(Color.RED);
        fontView.addBackgroundColor(Color.WHITE);
        

        【讨论】:

        • 它是否将字体对象缓存在内存中?在 Android 上处理字体时存在内存泄漏的已知可能性。
        【解决方案10】:

        还有另一个不错的解决方案,您可以直接在布局 xml 文件中使用,不需要使用 setTypeface

        是 Joan Zapata 的Iconify。您可以阅读here Iconify v2 中的新增功能。它包括 9 个不同的字体库,您可以通过向 build.gradle 文件添加依赖项来简单地使用它们。

        在布局 xml 文件中,可以在这些小部件之间进行选择:

        com.joanzapata.iconify.widget.IconTextview
        com.joanzapata.iconify.widget.IconButton
        com.joanzapata.iconify.widget.IconToggleButton
        

        【讨论】:

          【解决方案11】:

          最初创建asset文件夹并复制fontawesome图标(.ttf) 如何创建资产文件夹?

          app-->右键-->新建-->文件夹-->资产文件夹

          下一步下载如何下载.ttf文件? click here--> 并在下载解压后单击下载按钮并打开网络字体。最后选择真文本样式(ttf)粘贴资产文件夹。

          如何在android中设计xml和java文件?

          app-->res-->值 字符串.xml

          resources
              string name="calander_font" >&#xf073; <string
          resources
          

          这个例子多一种字体 Unicode click here

          Activity_main.xml

           <TextView
                  android:layout_width="30dp"
                  android:layout_height="30dp"
                  android:id="@+id/calander_view"/>
          

          MainActivity.java

          calander_tv = (TextView)findViewById(R.id.calander_view);
          
          Typeface typeface = Typeface.createFromAsset(getAssets(),"/fonts/fa-solid-900.ttf");
          calander_tv.setTypeface(typeface);
          calander_tv.setText(R.string.calander_font);
          

          输出:

          Output image

          【讨论】:

            【解决方案12】:

            我参加聚会有点晚了,但我写了一个自定义视图,让你这样做,默认情况下它设置为 entypo,但你可以修改它以使用任何图标字体:在这里查看:github.com/ MarsVard/IconView

            // 编辑库是旧的,不再支持... 新的https://github.com/MarsVard/IonIconView

            【讨论】:

            • 您的库没有缓存字体,而是在每次呈现视图时构建它。
            • @FernandoCamargo 是对的,这个库很旧,应该用更好的缓存来更新......这是性能更好的新库github.com/MarsVard/IonIconView
            【解决方案13】:

            如果你只需要几个字体很棒的图标,你也可以使用http://fa2png.io 来生成正常的像素图像。但是,如果您定期添加新图标/按钮,我会推荐 .ttf 版本,因为它更灵活。

            【讨论】:

              【解决方案14】:

              如果有人想知道如何以编程方式添加它,你必须这样做。

                 button_info.setText(R.string.icon_heart);
                  button_info.append(" Hallo"); //<<--- This is the tricky part
              

              【讨论】:

                【解决方案15】:

                因为所有答案都很好,但我不想使用库,每个解决方案都只有一行 java 代码,让我的 ActivitiesFragments 非常混乱。 所以我把TextView类改写如下:

                public class FontAwesomeTextView extends TextView {
                private static final String TAG = "TextViewFontAwesome";
                public FontAwesomeTextView(Context context) {
                    super(context);
                    init();
                }
                
                public FontAwesomeTextView(Context context, AttributeSet attrs) {
                    super(context, attrs);
                    init();
                }
                
                public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr) {
                    super(context, attrs, defStyleAttr);
                    init();
                }
                
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                public FontAwesomeTextView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
                    super(context, attrs, defStyleAttr, defStyleRes);
                    init();
                }
                
                private void setCustomFont(Context ctx, AttributeSet attrs) {
                    TypedArray a = ctx.obtainStyledAttributes(attrs, R.styleable.TextViewPlus);
                    String customFont = a.getString(R.styleable.TextViewPlus_customFont);
                    setCustomFont(ctx, customFont);
                    a.recycle();
                }
                
                private void init() {
                    if (!isInEditMode()) {
                        Typeface tf = Typeface.createFromAsset(getContext().getAssets(), "fontawesome-webfont.ttf");
                        setTypeface(tf);
                    }
                }
                
                public boolean setCustomFont(Context ctx, String asset) {
                    Typeface typeface = null;
                    try {
                        typeface = Typeface.createFromAsset(ctx.getAssets(), asset);
                    } catch (Exception e) {
                        Log.e(TAG, "Unable to load typeface: "+e.getMessage());
                        return false;
                    }
                
                    setTypeface(typeface);
                    return true;
                }
                }
                

                你应该做的是将字体ttf文件复制到assets文件夹中。并使用this cheat sheet查找每个图标字符串。

                希望这会有所帮助。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 2021-02-02
                  • 2021-01-09
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-10-21
                  • 2016-11-13
                  • 2022-08-08
                  • 2018-12-15
                  相关资源
                  最近更新 更多