Android 编程下字库的使用

在安卓操作系统下对于 TextView 字体的支持非常有限,默认情况下 TextView 的 typeface 属性支持 “Sans”,”serif”,”monospace” 这三种字体,如果在没有指定字体的情况下,系统缺省会使用 “Sans” 作为文本显示的字体。但这三种字体只支持英文,也就是说只要你显示的文字是中文,香港虚拟主机,美国服务器,无论你选择这三种字体中的哪一种,显示效果都是一样的。为此,谷歌又提供了另外一种字体的支持方式:字库。在 Android 2.2 版本中,网站空间,安卓对字库的支持是比较有限的,从 Android 2.3.3 版本开始,安卓系统已经能够支持大部分的字库。这意味着你在 SDK 版本为 Android 2.2 的平台下写的使用字库的代码只有在 Android 2.3.3 系统及其更的高版本中才能生效;如果你将代码运行在 Android 2.2 的系统上,你将不会看到自己所期望的效果,取而代之的是一片空白。下面来看一下代码实现过程,在代码实现实现之前,我们先来看下不同字库的显示效果,下图中第一张图:SIMKAI.TTF 简体字-楷体-常规、第二张图:MSYH.TTF 简体字-微软雅黑-常规。

1 package cn.sunzn.fonts; android.app.Activity; 4 import android.graphics.Typeface; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.widget.TextView;MainActivity extends Activity { TextView tv_title;12private TextView tv_author;13private TextView tv_line1;14private TextView tv_line2;15private TextView tv_line3;16private TextView tv_line4; onCreate(Bundle savedInstanceState) {19super.onCreate(savedInstanceState);20 setContentView(R.layout.activity_main);* 在 assets 目录下创建 fonts 文件夹并放入要使用的字体文件(**.ttf)并提供相对路径给23 * createFromAsset(AssetManager mgr, String path) 来创建 Typeface 对象,再通24 * 过TextView.setTypeface(Typeface tf) 指定文本显示的字体。Typeface face = Typeface.createFromAsset(getAssets(),”fonts/SIMFANG.TTF”);27 28tv_title = (TextView) findViewById(R.id.tv_title);29 tv_title.setTypeface(face);30 31tv_author = (TextView) findViewById(R.id.tv_author);32 tv_author.setTypeface(face);33 34tv_line1 = (TextView) findViewById(R.id.tv_line1);35 tv_line1.setTypeface(face);36 37tv_line2 = (TextView) findViewById(R.id.tv_line2);38 tv_line2.setTypeface(face);39tv_line3 = (TextView) findViewById(R.id.tv_line3);40 tv_line3.setTypeface(face);41 42tv_line4 = (TextView) findViewById(R.id.tv_line4);43 tv_line4.setTypeface(face);44 } onCreateOptionsMenu(Menu menu) {47 getMenuInflater().inflate(R.menu.activity_main, menu);;49 }50 51 }思念是对昨天悠长的沉淀和对未来美好的向往。

Android 编程下字库的使用

相关文章:

你感兴趣的文章:

标签云: