Typeface 指定字体样式

来源:互联网 发布:java最好用的版本 编辑:程序博客网 时间:2024/04/29 17:05

android.graphics.Typeface

 

  Typeface类是帮助描述一个字体对象,在TextView中通过使用setTypeface方法来制定一个输出文本的字体,其直接构造调用成员create方法可以直接指定一个字体名称和样式,比如

  static Typeface create(Typeface family, int style)

  static Typeface create(String familyName, int style)

  同时使用isBold和isItalic方法可以判断出是否包含粗体或斜体的字型。

  final boolean isBold()

  final boolean isItalic()

  该类的创建方法还有从apk的资源或从一个具体的文件路径,其具体方法为

  static Typeface createFromAsset(AssetManager mgr, String path)

  static Typeface createFromFile(File path)

  static Typeface createFromFile(String path)

 

 

例子1:

 

final  Typeface fontFace = Typeface.createFromAsset(getAssets(), "fonts/tahoma.ttf");
  tv_title=(TextView)this.findViewById(R.id.tv_title);

tv_title.setTypeface(fontFace);

 

例子2:

public void onDraw(Canvas canvas)
{
super.onDraw(canvas);
Paint p = new Paint();
String familyName = “宋体”;
Typeface font = Typeface.create(familyName,Typeface.BOLD);

p.setColor(Color.RED);
p.setTypeface(font);
p.setTextSize(22);
canvas.drawText(mstrTitle,0,100,p);
}
程序运行后,界面显示如下:

Paint

原创粉丝点击