Android 使用Font Awesome 顯示文字圖標

簡單幾步就能夠完成android

簡單的效果圖:web

1. 建立 assets 文件夾

在Android Studio 上的建立步驟爲:app

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

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

2. 編寫資源文件與代碼

/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);
相關文章
相關標籤/搜索