Android字体简介

Android系统默认支持三种字体,分别为:“sans”,“serif”,“monospace”。

android.graphic.typeface字体类:

本类的常量静态定义,首先为字体类型(typeface)名称
TypefaceDEFAULT
Typeface DEFAULT_BOLD
Typeface MONOSPACE
TypefaceSANS_SERIF
Typeface SERIF
字体风格(style)名称
intBOLD
int BOLD_ITALIC
int ITALIC
int NORMAL

设置TextView的字体可以通过TextView中的setTypeface方法来指定一个Typeface对象,因为Android的字体类

比较简单,我们列出所有成员方法:

  • staticTypeface create(Typeface family, int style)//静态方法,参数一为字体类型这里是Typeface的静态定义,如宋体,参数二风格,如粗体,斜体

  • staticTypeface create(String familyName, int style)//静态方法,参数一为字体名的字符串,参数二为风格同上,这里我们推荐使用上面的方法。

  • staticTypeface createFromAsset(AssetManager mgr, String path)//静态方法,参数一为AssetManager对象,主要用于从APK的assets文件夹中取出字体,参数二为相对于Android工程下的assets文件夹中的外挂字体文件的路径。

  • staticTypeface createFromFile(File path)//静态方法,从文件系统构造一个字体,这里参数可以是sdcard中的某个字体文件

  • staticTypeface createFromFile(String path) //静态方法,从指定路径中构造字体

  • staticTypeface defaultFromStyle(int style) //静态方法,返回默认的字体风格

  • intgetStyle() //获取当前字体风格

  • finalboolean isBold() //判断当前是否为粗体

  • finalboolean isItalic() //判断当前风格是否为斜体

例程:

1http://blog.csdn.net/wonderful19891024/archive/2010/11/24/6033304.aspx

2http://www.eoeandroid.com/thread-536-1-1.html


Android字体工作原理

    android字体由android 2D图形引擎skia实现,并在Zygote的Preloading classes中对系统字体进行load。相关文件有:skTypeface.cpp和skFontHost_android.cpp,其中后者是skia针对android平台字体实现的port。主要的变量有:struct FontInitRec {const char*         fFileName;const char* const*  fNames;     // null-terminated list};struct FamilyRec {FamilyRec*  fNext;SkTypeface* fFaces[5];};uint32_t gFallbackFonts[SK_ARRAY_COUNT(gSystemFonts)+1];load_system_fonts()@skFontHost_android.cpp load系统中所有的字体并给每种字体分配唯一的ID,并将字体分为两种:FamilyFonts和FallbackFonts,skPaint通过应用程序设置的字体(Typeface)所对应的ID最终实现字符的显示。

替换Android默认的汉字字体

    在android系统中,DroidSans是默认字体,只包含西方字符,应用程序默认情况下都会调用它,而DroidSansFallback包含了东亚字符,当需要显示的字符在DroidSans字体中不存在(如:汉字)时,即没有对应编码的字符时,系统会到DroidSansFallback中去找相应编码的字符,如果找到,则使用DroidSansFallback字体来显示它,如果仍找不到该编码对应的字符,则无法在屏幕上显示该字符。更换默认中文字体的步骤为:

1)将幼圆字体库youyuan.ttf重命名为DroidSansFallback.ttf,覆盖Android源码中frameworks/base/data/fonts目录下的DroidSansFallback.ttf文件;

2)重新编译Android系统;

3)编译SDK。生成的SDK中,android默认的中文字体已更换为幼圆字体。该方法的不足是删除了Android系统原来的中文字体。


为android系统添加一种默认字体,类似“sans”,“serif”,“monospace”

    在android系统中,默认的中文字体只有一种:DroidSansFallback.ttf,如果想在android应用程序中随意设置想要的中文字体,除了在应用程序中通过assets目录引入字体文件外,还可以通过增加android默认字体的方式来实现。添加步骤大致如下:1)在frameworks/base/data/fonts目录下添加字体文件,例如Driod-kaishu.ttf;2)在skia中增加楷书这一字体,需要修改的文件主要有skFontHost.cpp、skTypeface.cpp、Typeface.java等;3)在java层添加楷书字体相关API,需要修改的文件主要有typeface.java和textview.java;4)编译SDK5)将新生成的sdk导入eclipse,在eclipse中即可通过setTypeface(Typeface.KAISHU)和android:typeface=(“kaishu”)两种方式设置自己添加的字体。以增加楷书字体为例:==========main.xml=====================

<?xmlversion="1.0"encoding="utf-8"?>

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#f3243646"

>

android:id="@+id/TextView1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/normal"

android:typeface="normal"

android:textSize="25px"

/>

android:id="@+id/TextView2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/sans"

android:typeface="sans"

android:textSize="25px"

/>

android:id="@+id/TextView3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/serif"

android:typeface="serif"

android:textSize="25px"

/>

android:id="@+id/TextView4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/monospace"

android:typeface="monospace"

android:textSize="25px"

/>

android:id="@+id/TextView5"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:typeface="kaishu"

android:textSize="25px"

/>

==============String.xml=====================

<?xmlversion="1.0"encoding="utf-8"?>

(normal)字体测试!!!TypefacesTest!!!

Typefaces4

(kaishu)字体测试!!!TypefacesTest!!!

(sans)字体测试!!!TypefacesTest!!!

(serif)字体测试!!!TypefacesTest!!!

(monospace)字体测试!!!TypefacesTest!!!

==============Typefaces4.java=====================

packagecom.cy.Typefaces4;


importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.widget.TextView;


publicclassTypefaces4 extendsActivity{

/**Called when the activity is first created. */

@Override

publicvoidonCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}

}

结果图如下:


需要注意的是,在一个TextView里的所有字符只能设置成一种字体,比如说textview.settext(“字体测试!!!TypefacesTest!!!”)中的所有字符只能被设置成同一种字体,如果想将“字体测试!!!”设置为楷书,而“TypefacesTest!!!”设置为sans就必须将这两段字符用两个TextView显示,并对各自的字体分别进行设置。

==============main.xml=============

<?xmlversion="1.0"encoding="utf-8"?>

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffffff"

>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="kaishu"

/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/sans"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="sans"

/>

效果图如下:






 

更多相关文章

  1. android如何改变默认横竖屏方向
  2. react-native开发实例之替换默认logo——android实现
  3. android webview 设置
  4. Android使EditText和SearchView取消默认焦点
  5. android textview系统默认的颜色值是多少
  6. Android(安卓)关于WebView的相关属性
  7. android TabWidget 位置
  8. Android实现CBC加解密
  9. android如何调用显示和隐藏系统默认的输入法

随机推荐

  1. 新浪OAuth同步方案(测试成功)
  2. 在AndroidStudio中使用GreenDAO
  3. android两屏幕互相滑动
  4. android之App widget实际应用Demo
  5. usetc oj --Android(安卓)key
  6. 使用jQuery Mobile的注意事项
  7. Android(安卓)2.3状态栏中添加menu home
  8. Android自适应不同分辨率或不同屏幕大小
  9. 很有用的一篇文章,对于android新手,Log的分
  10. ubuntu10.04下在配置android与opencv2.2.