您的位置:首页 > 移动开发 > Android开发

android使用Font Awesome字体图标

2015-04-06 10:54 549 查看
Font Awesome的网址见http://fontawesome.dashgame.com/,Font Awesome提供可缩放的矢量图标,可以使用CSS所提供的所有特性对它们进行更改,包括:大小、颜色、阴影或者其它任何支持的效果。仅一个Font Awesome字库,就包含了与网页相关的所有形象图标。无论在任何尺寸下,可缩放的矢量图形都会为您呈现出完美的图标。Font Awesome的矢量图标,将使您的网站在视网膜级的高分屏上大放异彩。

那么,真强大的一个网页字体图标库可不可以用在安卓上呢,答案是可以的。

首先我们从文章前面的网址里下载最新的库,解压后再fonts目录下找到fontawesome-webfont.ttf,将其拷至我们的项目目录assets目录下,然后从github上下载一个字符串资源放到res目录下的values目录,网址见https://github.com/liltof/font-awsome-for-android

开始编码前看效果图




从效果图看出,我们没有用一张图片,就达到了这个效果,这一切都是依赖于字体,首先,创建自定义字体

Typeface font = Typeface.createFromAsset(getAssets(),
                "fontawesome-webfont.ttf");


然后是布局,简单布局了下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="${relativePackage}.${activityClass}" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="#fff" >
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#eee"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/tab1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/fa_android"
            android:textColor="#1fa67a"
            android:textSize="20sp" >
        </TextView>

        <TextView
            android:id="@+id/tab2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/fa_windows"
            android:textColor="#1fa67a"
            android:textSize="20sp" >
        </TextView>

        <TextView
            android:id="@+id/tab3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/fa_linux"
            android:textColor="#1fa67a"
            android:textSize="20sp" >
        </TextView>
    </LinearLayout>

</LinearLayout>


然后为每个TextView设置字体

private void initView() {
Typeface font = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
tab1 = (TextView) findViewById(R.id.tab1);
tab2 = (TextView) findViewById(R.id.tab2);
tab3 = (TextView) findViewById(R.id.tab3);

tab1.setTypeface(font);
tab2.setTypeface(font);
tab3.setTypeface(font);
}


如果你想改变图标大小和颜色,只要修改字体的大小和颜色即可,也就是说只要修改TextView的textSize和textColor。

更多使用效果待你去发现。

源码下载

http://download.csdn.net/detail/sbsujjbcy/8567213
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: