簡體   English   中英

如何在Native Android Application上使用“Font Awesome”中的圖標和符號

[英]How to use icons and symbols from “Font Awesome” on Native Android Application

我正在嘗試在我的應用程序上使用Font Awesome ,我能夠使用Typeface.createFromAsset()集成字體,但我也想使用這種字體提供的圖標,但到目前為止我還沒能做到那。

此特定字體包含Unicode專用區(PUA)內的圖標,用於媒體播放器控件,文件系統訪問,箭頭等內容。

有沒有人在Android上使用包含圖標和符號的字體,這有可能嗎?

在我的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> 
  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); 

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

也許為時已晚,但我有同樣的需求,所以我發布了這個https://github.com/liltof/font-awsome-for-android這是一個Android准備好的xml版本的字體真棒可用就像Keith Corwin說的

希望它會幫助別人。

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

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(getString((char)valLong+"");

為此目的設計了一個小而有用的

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 DrawableDrawable

<?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);

鏈接:

如果你想要編程的setText而不添加字符串到string.xml

在這里看到它的十六進制代碼:

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

替換&#xf066; 到0xf066

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

我用於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"

我在C#(Xamarin)中創建了這個幫助類,以編程方式設置text屬性。 它對我來說非常好用:

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。 希望它可以幫到某人!

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

還有另一個很好的解決方案,您可以直接在布局xml文件中使用,而不需要使用setTypeface

這是Joan Zapata的Iconify 你可以在這里閱讀Iconify v2中的新功能。 它包含9種不同的字體庫,您可以通過向build.gradle文件添加依賴項來簡單地使用它們

在布局xml文件中,可以在這些小部件之間進行選擇:

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

最初創建資產文件夾並復制fontawesome圖標(.ttf)如何創建資產文件夾?

app - >右鍵單擊 - >新建 - >文件夾 - >資產文件夾

下一步下載如何下載.ttf文件? 單擊此處 - >並在下載提取並打開Web字體后單擊下載按鈕。 最后選擇真正的文本樣式(ttf)粘貼資產文件夾。

如何在android中設計xml和java文件?

app - > res - > values string.xml

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

這個例子的一個字體更多Unicode 點擊這里

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

輸出:

輸出圖像

我有點遲到了,但是我寫了一個自定義視圖讓你這樣做,默認它設置為entypo,但你可以修改它以使用任何iconfont:在這里查看:github.com/MarsVard/IconView

//編輯圖書館已經過時了,不再支持了...這里有一個新的https://github.com/MarsVard/IonIconView

如果您只需要幾個字體真棒圖標,您還可以使用http://fa2png.io生成普通像素圖像。 但是如果你經常添加新的圖標/按鈕,我建議.ttf版本更靈活。

如果有人想知道如何添加它programmitcally你必須這樣做。

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

由於所有的答案都很棒,但我不想使用庫,每個解決方案只有一行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文件夾。並使用此備忘單查找每個圖標字符串。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM