ITeye博客

android 字体样式

Tank03 · 2011-12-30 08:52 · android · 移动开发 · android字体样式 · android 修改字体样式 · Markdown

前些天,弄了个小程序.总觉得他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); 
		} 
	}
}

 

 

  本来附上字体的,但是网速贼慢.大伙自己去下把.要的,可以留个邮箱.

评论(5)

  1. #5 楼 · 一级苦工 · 2013-04-19 14:22

    谢谢了!非常好!

  2. #4 楼 · Tank03 · 2013-04-18 17:37

    一级苦工 writes:

    可以把字体样式发给我一份吗,912917891@qq.com,谢谢啦!

    兄弟,太大了,给你个连接:http://www.jbdown.com/soft/36990.html(Android全字体合集(29种.ttf 字体打包))

  3. #3 楼 · 一级苦工 · 2013-04-17 09:31

    可以把字体样式发给我一份吗,912917891@qq.com,谢谢啦!

  4. #2 楼 · Tank03 · 2012-03-04 11:57

    huangzubin13 writes:

    求图 求真相

    你测试一下。不就完了。

  5. #1 楼 · huangzubin13 · 2012-02-21 18:05

    求图 求真相

评论功能维护中,当前仅展示历史评论。