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

Android 字体库的使用。引入外部字体

2017-09-19 14:24 435 查看
原文地址:http://blog.csdn.net/zuiwuyuan/article/details/49454411

开发Android的人大多都知道,Android里面对字体的支持少得可怜,默认情况下,TextView  的 typeface 属性支持 "Sans","serif","monospace" 这三种字体,如果在没有指定字体的情况下,系统缺省会使用 "Sans" 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,无论你选择这三种字体中的哪一种,显示效果都是一样的。

  但这对开发一款精致的APP来说,或许是不够的,毕竟,咱们大家都喜欢用高端大气上档次的字体,吸引用户的眼球。

 先给给大家呈现效果图:



  是不是字体变化的更上档次了,看下实现吧:

[java] view
plain copy

import android.app.Activity;  

import android.graphics.Typeface;  

import android.os.Bundle;  

import android.widget.TextView;  

  

public class MainActivity extends Activity {  

  

    private TextView text1;  

    private TextView text2;  

    private TextView text3;  

    private TextView text4;  

    private TextView text5;  

  

    @Override  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

  

        setContentView(R.layout.activity_main);  

  

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

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

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

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

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

  

  

        Typeface typeFace1 = Typeface.createFromAsset(getAssets(), "fonts/huaxing.ttf");  

  

        Typeface typeFace2 = Typeface.createFromAsset(getAssets(), "fonts/Helvetica.ttf");  

  

        Typeface typeFace3 = Typeface.createFromAsset(getAssets(), "fonts/simkai.ttf");  

  

        Typeface typeFace4 = Typeface.createFromAsset(getAssets(), "fonts/huacai.TTF");  

  

        Typeface typeFace5 = Typeface.createFromAsset(getAssets(), "fonts/fangxiao.TTF");  

  

        text1.setTypeface(typeFace1);  

        text2.setTypeface(typeFace2);  

        text3.setTypeface(typeFace3);  

        text4.setTypeface(typeFace4);  

        text5.setTypeface(typeFace5);  

    }  

}  

  有人搞不清这些字体库文件放在哪,即"fonts/fangxiao.TTF"在哪,给大家看下目录:



  好啦,就是这么easy!

  

  注: 这里的.ttf  大小写一定要区分好。。是什么就怎么引入。字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如我用了仿宋,APP增加了2.5M;用了宋体,app增加了3M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: