android设置各种字体样式

文章为转载,原文出处:http://tank03.iteye.com/blog/1329905

 

  前些天,弄了个小程序.总觉得他Y的手机字体太难看.然后在网上死命的查.原来方法贼简单.差点被忽悠去替换系统文件.废话不说.上程序代码片段.

  在贴代码之前,请你注意2点:

  1.你的手机最好是1500¥以上的,.因为好点的手机更加兼容你导入的字体.如果你的手机没有显示你的字体,也没有报错,恭喜你,你的手机out了.

  2.你导入的文件名称必须是字母,不能中文.而且你的字体文件必须放在你项目的  assets/fonts 下面.

 

 

Java代码 复制代码  收藏代码
  1. TextView titleView=new TextView(this);   
  2.   
  3.     Typeface face = Typeface.createFromAsset(getAssets(),"fonts/DS.ttf");   
  4. titleView.setText("我显示");//   
  5. titleView.setTypeface(face);  
		TextView titleView=new TextView(this);
		
	  	Typeface face = Typeface.createFromAsset(getAssets(),"fonts/DS.ttf");
		titleView.setText("我显示");//
		titleView.setTypeface(face);
 

 

代码很简单.下面给个封装类.

 

Java代码 复制代码  收藏代码
  1. import android.app.Activity;    
  2. import android.content.Context;    
  3. import android.graphics.Canvas;    
  4. import android.graphics.Color;    
  5. import android.graphics.Paint;    
  6. import android.graphics.Typeface;    
  7. import android.os.Bundle;    
  8. import android.view.View;    
  9.   
  10. /**  
  11.  *   
  12. *      
  13. * 项目名称:mip     
  14. * 类名称:Typefaces     
  15. * 类描述: 字体样式  
  16. * 创建人:JIE  
  17. * 创建时间:2011-12-29 上午11:51:05     
  18. * 修改人:Administrator     
  19. * 修改时间:2011-12-29 上午11:51:05     
  20. * 修改备注:     
  21. * @version      
  22. *  
  23.  */  
  24. public class Typefaces extends Activity    
  25. {    
  26.     /** Called when the activity is first created. */    
  27.     @Override    
  28.     public void onCreate(Bundle savedInstanceState)    
  29.     {    
  30.         super.onCreate(savedInstanceState);    
  31.        
  32.         setContentView(new SampleView(this));    
  33.     }    
  34.        
  35.     private static class SampleView extends View    
  36.     {    
  37.         private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    
  38.         private Typeface mFace;    
  39.        
  40.         public SampleView(Context context)    
  41.         {   
  42.             super(context);    
  43.             //实例化自定义字体    
  44.             mFace = Typeface.createFromAsset(getContext().getAssets(),"fonts/a.ttf");    
  45.             //设置字体大小    
  46.             mPaint.setTextSize(18);    
  47.         }    
  48.            
  49.         @Override    
  50.         protected void onDraw(Canvas canvas)    
  51.         {   
  52.             canvas.drawColor(Color.WHITE);    
  53. //          //绘制默认字体    
  54. //          mPaint.setTypeface(null);    
  55. //          canvas.drawText("Default:abcdefg", 10, 100, mPaint);    
  56.             //绘制自定义字体    
  57.             mPaint.setTypeface(mFace);    
  58.             canvas.drawText("Custom:abcdefg"10200, mPaint);    
  59.         }    
  60.     }   
  61. }  

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值