(三十七)Android开发中修改程序字体

 1、在Android XML文件中设置系统默认的字体
  可以在XML文件中采用android:typeface设置字体,例如android:typeface=”monospace”。在这里例子中我们在Activity中对android:text=”Hello, World! 您好”分别进行了四种显示方式,依次为“Sans”,“serif”,“monospace”和系统缺省方式(经试验缺省采用采用sans)。英文字体有差异,貌似中文字体没有差异。XML文件如下:

 

 <?xml version=“1.0″ encoding=”utf-8″?> 
  <TableLayout android:stretchColumns = “1”>
  <TableRow>
  <TextView android:text=“sans:” 
  android:layout_marginRight=“4px” 
  android:textSize=“20sp” />

  <TextView android:text=”Hello, World! 您好”
  android:typeface =“sans” <!– android:typeface用于指定字体–> 
  android:textSize=“20sp” />
  </TableRow>

  <TableRow> 
  <TextView android:text=“custom:”  /> 
  <TextView android:id=“@+id/c12_custom” 
  android:text=“Hello, World! 您好” 
  android:textSize=“20sp” /> 
  </TableRow> 
  </TableLayout>

 

  2、使用其他字体

  1)将新字体的TTF文件copy到assets/fonts/目录下面,例如我们将“*.ttf”copy了过去。
  2)我们需要将widget设置为该字体,比较遗憾的是,不能直接在XML文件中进行,需要编写源代码。

 

 TextView tv = (TextView)findViewById(R.id.c12_custom);
  //从assert中获取有资源,获得app的assert,采用getAserts(),通过给出在assert/下面的相对路径。在实际使用中,字体库可能存在于SD卡上,可以采用createFromFile()来替代createFromAsset。 
 Typeface face = Typeface.createFromAsset (getAssets() , “fonts/timesi.ttf” );
 tv.setTypeface (face);

  在模拟器中先后导入华文行楷的字体,大约4M,但是系统无法识别出该字体,没有显示,然后尝试使用英文字体timesi.ttf,正常。因此Android并非和所有的TTF字体都能兼容,尤其在中文特殊字体的支持会存在问题,对于不兼容的字体,Android不出报错,只是无法正常显示。一般而言我们都会使用系统缺省提供的体。

  对于华文行楷字体,我们一开始使用的文件是中文名字,出现报错,后来我们将之改为全小写的英文名称就不会出错,所以在文件命名上需要注意。

3、一些注意
  使用其他字库,都会消耗程序的空间,这是要非常注意的。而且这些字库有时并不能完全提供你所需要的文字。
  举个例子,省略方式。当文字太多的时候,可以通过省略号省略后面的内容,省略号是使用“…”作为一个字体,可通过android:ellipsize属性进行设置。如果我们需要使用省略功能,需要确保字体具有省略号。此外,为了保证长度的一直,Android会进行填充处理,除了将一个字符更换为省略符合外,后面的字符将更换为一个特殊的Unicode字符,‘ZERO WIDTH NO-BREAK SPACE’ (U+FEFF)。这个字符并占用任何可视的位置,但是保障了string具有同样的长度。不是所有的字体都支持这个特殊的字符,可能会引发一些乱码现象。
  Android是支持国际语言的,但是我们仍需要对custom的字体小心处理。

4、如果一个程序中多处需要修改字体,可以通过使用自定义的textView,代码如下所示(2)所示,在里面定义方法定义字体,以免N多地方都要使用setTypeface设置字体,非常麻烦。
使用时的代码如表(1)所示

表(1):
<com.cn21.esafe.utils.CustomFontTextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:gravity="center"
        android:textColor="@color/white"
        android:textSize="@dimen/text_title" />    
表(2):
package com.cn21.esafe.utils;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;
public class CustomFontTextView extends TextView {
    private static Typeface typeface;
    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        setCustomTypeface(context);
    }
    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomTypeface(context);
    }
    public CustomFontTextView(Context context) {
        super(context);
        setCustomTypeface(context);
    }
    private void setCustomTypeface(Context context) {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(context.getAssets(),
                    "fonts/fangzhenglanting.ttf");
        }
        setTypeface(typeface);
    }
    public static Typeface getTypeface(Context context) {
        if (typeface == null) {
            typeface = Typeface.createFromAsset(context.getAssets(),
                    "fonts/fangzhenglanting.ttf");
        }
        return typeface;
    }
}

 

posted @ 2015-01-04 09:24  小菜美妞成长中  阅读(1329)  评论(0编辑  收藏  举报