作為一個Android開發者,自己想做一個app練手,有個比較頭疼的問題就是沒有UI圖標資源~~ 其實很容易搞定的,下面就來聊聊如何在Android中應用圖標字體庫,找圖標不再糾結!

fd81fbf8ef38fd0837073279b7700fe4.png

1、點擊左上角菜單 -> Manager Projects 進入管理頁面。

e6d9e419169df28c53cad3c364545fbd.png

2、點擊New Project, 創建一個工程,如First App並點擊Load>

cf1646aa46bd124b43a2ff3a33550fd7.png

3、點擊Add Icon From Libray,去選擇自己喜歡的Library,點擊+Add添加到工程里面。Library有收費的,也有免費的,視情況而定。

bbfd9bb781b01c289f1bdcc5b3ac6250.png

98d98b460af79626499539ac8112c21b.png

4、轉到資源頁面。選擇自己想要下載的圖標,怎么都是灰色的?安啦,后面有驚喜!

c546c469800f965bdeb404a4f80b267f.png

5、點擊右下角Generate Font,生成ttf字體庫。

d29166faaa2ee385e515a6369fa6f328.png

上面四個圖標就是我前面選中的,下面的諸如e911文字就是圖標對應的unicode符號,中文字體也是這么一個道理。點擊download下載字體庫。

24e50710a36c1563331660f5b7050156.png

6、下載完成,解壓。拷貝fonts/icomoon.ttf 到 android-assets/fonts 下面。

ae2899f87ad261f1143e04091c75f350.png

7、應用字體。首先建一個字體“映射”類,反正官方不太推薦用枚舉方式,暫且就用注解吧~~ 打開剛才解壓包里面的demo.html,對應來創建字體映射。

import android.support.annotation.StringDef;

/**

* @author yuyh.

* @date 2016/11/10.

*/

@StringDef({

MDFonts.HOME,

MDFonts.NEWSPAPER,

MDFonts.MUSIC,

MDFonts.PLAY

})

public @interface MDFonts {

String HOME = "\ue902";

String NEWSPAPER = "\ue904";

String MUSIC = "\ue911";

String PLAY = "\ue912";

}

import android.content.Context;

import android.graphics.Typeface;

import android.widget.TextView;

/**

* @author yuyh.

* @date 2016/11/10.

*/

public class MDFontsUtils {

public static Typeface OCTICONS;

/**

* Get octicons typeface

*

* @param context

* @return octicons typeface

*/

public static Typeface getOcticons(final Context context) {

if (OCTICONS == null)

OCTICONS = getTypeface(context, "fonts/icomoon.ttf");

return OCTICONS;

}

/**

* Set octicons typeface on given text view(s)

*

* @param textViews

*/

public static void setOcticons(final TextView... textViews) {

if (textViews == null || textViews.length == 0)

return;

Typeface typeface = getOcticons(textViews[0].getContext());

for (TextView textView : textViews)

textView.setTypeface(typeface);

}

/**

* Get typeface with name

*

* @param context

* @param name

* @return typeface

*/

public static Typeface getTypeface(final Context context, final String name) {

return Typeface.createFromAsset(context.getAssets(), name);

}

}

9、圖標對應是用TextView表示,而不是ImageView。如下:

android:id="@+id/tvMusic"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:textSize="16dp" />

android:id="@+id/tvHome"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_margin="10dp"

android:textSize="16dp" />

在Java中應用字體。如下:

tvMusic = (TextView) findViewById(R.id.tvMusic);

tvMusic.setText(MDFonts.MUSIC);

tvHome = (TextView) findViewById(R.id.tvHome);

tvHome.setText(MDFonts.HOME);

// 應用字體

MDFontsUtils.setOcticons(tvMusic, tvHome);

run起來,大功告成!

16d485e9884140eab0aff1bff82da476.png

10、你會發現,run起來圖標顏色全是Android默認的灰色,那么怎么更改圖標顏色呢?剛才說了,圖標字體用的是TextView,實際上他跟中文英文字體沒什么兩樣,他本質上還是文字。所以,TextView怎么設置字體大小、字體顏色,這里就對應怎么設置。如下:

tvHome.setTextColor(Color.RED);

tvHome.setTextSize(50);

6b6d7f520054f1cee42a136795e2cd5f.png

哈哈,沒毛病!

當然,也可以把字體符號配置在string.xml

\ue902

// 當然,還需要下面這步來應用設置MDFontsUtils.setOcticons(tvHome);

更多用法大家就自行擴展吧!比如可以自定義一個TextView,直接應用字體,就不需要MDFontsUtils.setOcticons(tvHome); 這步操作了,自己用就可以啦!

感謝閱讀!

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐