android使用字体图标ttf

所以下面我要介绍的就是我们现在要介绍的字体图标了。而什么是字体图标呢?说的简单点字体图标是指将图标做成字体文件(.ttf),从而代替传统的png等图标资源。
优点:

    1. 可以高度自定义图标的样式(包括大小和颜色),对于个人开发者尤其适用
    1. 可以减少项目和安装包的大小(特别你的项目中有很多图片icon时,效果将是M级)
    1. 几乎可以忽略屏幕大小和分辨率,做到更好的适配
    1. 使用简单
      ...............

缺点:

    1. 只能是一些简单的icon,不能代替如背景图、9图等资源
    1. 一些需要文字说明的icon,图片资源将会是更好的选择
    1. 对设计的要求更高,不同icon可能拥有不同的边距,这时要切换icon时还要设置大小
    1. 由于边距的存在可能存在无法填满控件的情况
    1. 无法在android studio中进行实时预览
      ……

但是总体对我们开发者而言,这个字体图标的利还是大于弊,就一个使用很方便,并且屏幕自适应和较少apk的大小就能够让我们难以割舍了。

二.字体图标使用

1.生成.ttf格式的字体文件

这里我一般获取字体图标都是通过阿里巴巴矢量图标库因为这里面不仅有很多各式各样的字体图标,并且很多都十分适合开发者,用起来也很方便。

字体图标样式

下载字体图标里之后生成的zip,解压之后会有这样的一个文件

ttf格式的字体图标

2.字体文件的配置

把它放到android的assets文件夹底下。







iconfont.ttf位置

我在工程中怎么找到这些图标?

正如前面所说 我们其实已经知道怎么来代表一个图标了,但是还是需要知道怎么在工程中用,其实也很简单我们在res --> values --> string.xml中添加字符串如图:











string

添加样式

<string name="font1">&#xe665;</string>

这个样式是如何获取的呢



这个样式是如何获取的呢

很简单就是点击如图复制代码,再在string中粘贴即可。





string中的字符串

3.字体文件的使用

TextView中

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <!-- activity_main.xml 中用来显示图标 -->
    <TextView
        android:id="@+id/tvShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

在activity中使用Typeface来进行字体文件格式的调用

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        TextView tvShow = (TextView) findViewById(R.id.tvShow);
        Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf"); 
        tvShow.setTypeface(font);
        tvShow.setText(getResources().getString(R.string.font1));
    }
}

这些图标怎么设置颜色和具体大小?

就相当于对这个其实变成了字一样进行其TextView的文字的颜色大小设置一样

可以在xml中进行设置

<TextView
        android:id="@+id/tvShow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:gravity="center"
        android:textColor="#00ff00"
        android:textSize="80sp"/>

也可以动态用代码在activity中设置

tvShow.setTextSize(80); //设置大小
tvShow.setTextColor(Color.parseColor("#00ff00")); //设置颜色

三.自定义FontIconView来进行代码的代码优化

  • 理解的上面的我们就可以自定义一个字体图标的控件 FontIconView
  • 在工程目录下新建一个继承TextView 的class 起名为FontIconView
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 
 * 自定义字体图标
 * @author M.Z
 *
 */
public class FontIconView extends TextView{

    
    /*
     * 控件在xml加载的时候是调用两个参数的构造函数 ,为了自定义的控件的完整性我们可以
     * 都把构造函数写出来
     */
    public FontIconView(Context context) {
        super(context);
        init(context);
    }
    
    public FontIconView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public FontIconView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * 初始化
     * @param context
     */
    private void init(Context context) {
        //设置字体图标
        Typeface font = Typeface.createFromAsset(context.getAssets(), "iconfont.ttf"); 
        this.setTypeface(font);
    }
}

xml中使用

<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" >
    
    <com.example.views.FontIconView
        android:id="@+id/fontView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        />

</LinearLayout>

动态代码设置

 FontIconViewfontView= (FontIconView) findViewById(R.id.fontView);

        //必须使用strings.xml文件下面的变量解析,直接在java代码中写字体图标字符串无效         

getResources().getString(fontView.setText(R.string.font1)); 
    fontView.setTextSize(15); fontView.setTextColor(Color.parseColor("#ff0000")); //设置颜色 
   

这样会不会发现以前比较困难的图片自适应问题是不是变得迎刃而解了,并且在这之中,我们会发现这个自定义FontIconView之后代码的使用率也很高,并且什么样的颜色和大小的只要简简单单的设置一下就好了,所以相信对开发者而言,这种功能性十分方便,并且开发过程也变得简单多了。




作者:想飞的喵星人
链接:https://www.jianshu.com/p/06365b31dff1
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

猜你喜欢

转载自blog.csdn.net/kunga0814/article/details/80378979