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"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#f3243646"

>

<TextView

android:id="@+id/TextView1"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/normal"

android:typeface="normal"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView2"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/sans"

android:typeface="sans"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView3"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/serif"

android:typeface="serif"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView4"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/monospace"

android:typeface="monospace"

android:textSize="25px"

/>

<TextView

android:id="@+id/TextView5"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:typeface="kaishu"

android:textSize="25px"

/>

</LinearLayout>

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

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

<resources>

<stringname="normal">(normal)字体测试!!!TypefacesTest!!!</string>

<stringname="app_name">Typefaces4</string>

<stringname="kaishu">(kaishu)字体测试!!!TypefacesTest!!!</string>

<stringname="sans">(sans)字体测试!!!TypefacesTest!!!</string>

<stringname="serif">(serif)字体测试!!!TypefacesTest!!!</string>

<stringname="monospace">(monospace)字体测试!!!TypefacesTest!!!</string>

</resources>

==============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"?>

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#ffffffff"

>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/kaishu"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="kaishu"

/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/sans"

android:textSize="30px"

android:textColor="#ff000000"

android:typeface="sans"

/>

</LinearLayout>

效果图如下:






更多相关文章

  1. android json解析
  2. Android(安卓)时间获取总结
  3. 根据文件名称修改安卓默认的蓝牙接收文件地址
  4. Android(安卓)MTK 修改默认音频和声音大小
  5. Android实现简单字符驱动程序及测试
  6. 重拾Android之路之开发准备
  7. 集成第三方库到android程序方法
  8. Android(安卓)控制EditText输入为英文或数字
  9. 如何修改Android中Browser的UserAgent

随机推荐

  1. Android(安卓)签名证书
  2. android 编译问题解决
  3. Android:在WebView中获取网页源码
  4. Android(安卓)--- 取得电话簿信息,并保存为Vc
  5. android之camera2获取数据
  6. android JNI简单的C调java
  7. android监听联系人变化的方法
  8. Android通过onTouch事件实现单击,双击,长按
  9. Android(安卓)提示框
  10. Android(安卓)RecyclerView 子条目(item)嵌