Android 字体相关总结 android TextView设置中文字体加粗实现方法

1、Android系统默认支持三种字体,分别为:“sans”, “serif”,  “monospace“  系统缺省方式(经试验缺省采用采用sans);

2、在Android中可以引入其他字体

3、示例如下:

4、布局文件

main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout    xmlns:andro></TextView>

         <!--  使用默认的sans字体--> 
        <TextView    android:></TextView>

         <!--  使用默认的serifs字体--> 
        <TextView    android:></TextView>

          <!--  使用默认的monospace字体--> 
        <TextView    android:></TextView>
    </TableRow>  

  <!--  这里没有设定字体,我们将在Java代码中设定-->       
    <TableRow>
        <TextView    android:text="custom:"
                    android:layout_marginRight="4px"
                    android:textSize="20sp"></TextView>
        <TextView    android:></TextView>
    </TableRow>                
</TableLayout>

5、Java代码

package yyl.fonts;

import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;

public class FontsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //得到TextView控件对象
        TextView textView = (TextView)findViewById(R.id.custom);

        //将字体文件保存在assets/fonts/目录下,创建Typeface对象 
        Typeface typeFace = Typeface.createFromAsset(getAssets(), "fonts/HandmadeTypewriter.ttf");

        //应用字体
        textView.setTypeface(typeFace); 
    }
}

英文设置加粗可以在xml里面设置: 

代码如下:

<SPAN style="FONT-SIZE: 18px">android:textStyle="bold"</SPAN> 


英文还可以直接在String文件里面直接这样填写: 

代码如下:

<string name="styled_text">Plain, <b>bold</b>, <i>italic</i>, <b><i>bold-italic</i></b></string> 


b代码加粗,i代表倾斜 
中文设置加粗就需要在代码中获取到当前TextView在进行设置: 

代码如下:

TextView tv = (TextView)findViewById(R.id.tv); 
TextPaint tp = tv.getPaint(); 
tp.setFakeBoldText(true);