可以通过自己往assets/fonts/里面添加自己喜欢的字体,也可以利用系统内置的字体,textView.setTypeface(Typeface.SANS_SERIF);
1、在Android 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,分别将sans 修改为serif,monospace … …
<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不出报错,只是无法正常显示。一般而言我们都会使用系统缺省提供的字体。
对于华文行楷字体,我们一开始使用的文件是中文名字,出现报错,后来我们将之改为全小写的英文名称就不会出错,所以在文件命名上需要注意。
-----------------------------------------------------------------------------------------------------------------------------------------------
Android 对中文字体支持很不好~~ 需要加入相应的字体库
(1)创建布局Layout
//创建线性布局
LinearLayout linearLayout=newLinearLayout(this);
//设定线性布局为垂直方向
linearLayout.setOrientation(LinearLayout.VERTICAL);
//以该线性布局做视图
setContentView(linearLayout);
(2)针对正常字体
//普通正常字体
normal=newTextView(this);
//设置字体内容,请注意:目前Android主要针对拉丁语系可使用字型设定,中文暂不支持
normal.setText("Normal Font FYI");
//设置字体大小
normal.setTextSize(20.0f);
//设置字型为默认,正常字体
normal.setTypeface(Typeface.DEFAULT,Typeface.NORMAL);
//增加该字体并显示到布局linearLayout中
linearLayout.addView(normal,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(3)针对粗体字体
//粗体字体
bold=newTextView(this);
bold.setText("Bold Font FYI");
bold.setTextSize(20.0f);
//设置字体颜色为蓝色
bold.setTextColor(Color.BLUE);
//设置字型为默认粗体,粗体字体
bold.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
linearLayout.addView(bold,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(4)针对斜体字体
//斜体字体
italic=newTextView(this);
italic.setTextSize(20f);
italic.setText("Italic Font FYI");
//设置字体颜色为红色
italic.setTextColor(Color.RED);
//设置字型为等宽字型,斜体字体
italic.setTypeface(Typeface.MONOSPACE,Typeface.ITALIC);
linearLayout.addView(italic,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(5)针对粗斜体字体
//粗斜体字体
italic_bold=newTextView(this);
italic_bold.setTextSize(20f);
italic_bold.setText("Italic & Bold Font FYI");
//设置字体颜色为黄色
italic_bold.setTextColor(Color.YELLOW);
//设置字型为等宽字型,斜体字体
italic_bold.setTypeface(Typeface.MONOSPACE,Typeface.BOLD_ITALIC);
linearLayout.addView(italic_bold,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(6)针对中文仿“粗体”
//针对Android字型的中文字体问题
chinese=newTextView(this);
chinese.setText("中文粗体显示效果");
//设置字体颜色
chinese.setTextColor(Color.MAGENTA);
chinese.setTextSize(20.0f);
chinese.setTypeface(Typeface.DEFAULT_BOLD, Typeface.BOLD);
//使用TextPaint的仿“粗体”设置setFakeBoldText为true。目前还无法支持仿“斜体”方法
tp=chinese.getPaint();
tp.setFakeBoldText(true);
linearLayout.addView(chinese,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
(7)自定义创建字型
//自定义字体字型
custom=newTextView(this);
//字体MgOpenCosmeticaBold.ttf放置于assets/font/路径下
typeface=Typeface.createFromAsset(getAssets(),"font/MgOpenCosmeticaBold.ttf");
custom.setTypeface(typeface);
custom.setText("Custom Font FYI");
custom.setTextSize(20.0f);
//设置字体颜色
custom.setTextColor(Color.CYAN);
linearLayout.addView(custom,newLinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));