zoukankan      html  css  js  c++  java
  • Android 字体库的使用

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

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

     先给给大家呈现效果图:



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

    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!

      

      注: 字体库虽然很好,但着实不建议使用,why?因为字体库的体积实在庞大,一个简单的库,比如楷体,都要4M,所以,一般来说不建议使用字体库,除非你的app风格大多要求使用该字体,否则完全没有必要引用字体库,得不偿失!

  • 相关阅读:
    css3实现酷炫的3D盒子翻转效果
    Java源码学习:HashMap实现原理
    mac 如何显示隐藏文件和.点开头文件?
    mac 下 安装 mongodb 数据库
    WPF 自定义TextBox
    Appium+Python 安卓APP自动化测试:安装app和卸载app
    Appium+Python 安卓APP自动化测试:环境搭建与基础操作
    DataFrame利用函数或映射进行数据转换map
    TypeError: drop_duplicates() got an unexpected keyword argument 'take_last'
    DataFrame合并:合并重叠数据combine_first
  • 原文地址:https://www.cnblogs.com/hehe520/p/6329955.html
Copyright © 2011-2022 走看看