Android 使用Font Awesome 顯示文字圖標(biāo)

簡(jiǎn)單幾步就可以完成

簡(jiǎn)單的效果圖:

1. 創(chuàng)建 assets 文件夾

在Android Studio 上的創(chuàng)建步驟為:

src/main上右鍵 --> New --> Folder --> Assets Folder.

將FontAwesome 字體文件copy到assets指定的路徑,這里我放在assets/font/fontawesome-webfont.ttf.

2. 編寫(xiě)資源文件與代碼

/values/strings.xml

<string name="fa_car">&#xf1b9;</string>
<string name="fa_apple">&#xf179;</string>
<string name="fa_android">&#xf17b;</string>

activity_layout.xml

//...
<TextView
    android:id="@+id/tv_1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/fa_car"
    android:textSize="20sp"
    android:textColor="@color/cardview_shadow_start_color"
    />
<TextView
    android:id="@+id/tv_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/fa_apple"
    android:textSize="24sp"
    android:textColor="@color/colorPrimaryDark"
    />
<TextView
    android:id="@+id/tv_3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/fa_android"
    android:textSize="48sp"
    android:textColor="@color/colorAccent"
    />
//...

Activity類

    TextView tv_1 = (TextView)findViewById(R.id.tv_1);
    TextView tv_2 = (TextView)findViewById(R.id.tv_2);
    TextView tv_3 = (TextView)findViewById(R.id.tv_3);

    //獲取assets文件夾里的字體文件
    Typeface font = Typeface.createFromAsset(getAssets(), "font/fontawesome-webfont.ttf");

    //給指定的TextView加載字體
    tv_1.setTypeface(font);
    tv_2.setTypeface(font);
    tv_3.setTypeface(font);

3. 最后附上字體下載鏈接

字體:http://fontawesome.io/

對(duì)照表:http://fontawesome.io/cheatsheet/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容