前些天,弄了个小程序.总觉得他Y的手机字体太难看.然后在网上死命的查.原来方法贼简单.差点被忽悠去替换系统文件.废话不说.上程序代码片段.
在贴代码之前,请你注意2点:
1.你的手机最好是1500¥以上的,.因为好点的手机更加兼容你导入的字体.如果你的手机没有显示你的字体,也没有报错,恭喜你,你的手机out了.
2.你导入的文件名称必须是字母,不能中文.而且你的字体文件必须放在你项目的 assets/fonts 下面.
TextView titleView=new TextView(this);
Typeface face = Typeface.createFromAsset(getAssets(),"fonts/DS.ttf");
titleView.setText("我显示");//
titleView.setTypeface(face);
代码很简单.下面给个封装类.
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
/**
*
*
* 项目名称:mip
* 类名称:Typefaces
* 类描述: 字体样式
* 创建人:JIE
* 创建时间:2011-12-29 上午11:51:05
* 修改人:Administrator
* 修改时间:2011-12-29 上午11:51:05
* 修改备注:
* @version
*
*/
public class Typefaces extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View
{
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
private Typeface mFace;
public SampleView(Context context)
{
super(context);
//实例化自定义字体
mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/a.ttf");
//设置字体大小
mPaint.setTextSize(18);
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawColor(Color.WHITE);
// //绘制默认字体
// mPaint.setTypeface(null);
// canvas.drawText("Default:abcdefg", 10, 100, mPaint);
//绘制自定义字体
mPaint.setTypeface(mFace);
canvas.drawText("Custom:abcdefg", 10, 200, mPaint);
}
}
}
本来附上字体的,但是网速贼慢.大伙自己去下把.要的,可以留个邮箱.