Android 设置字体的三种方法(TypeFace)

http://blog.csdn.net/legend12300/article/details/69875816

Android系统默认字体支持四种字体,分别为:

  1. noraml (普通字体,系统默认使用的字体)
  2. sans(非衬线字体)
  3. serif (衬线字体)
  4. monospace(等宽字体)

除此之外还可以使用其他字体文件(*.ttf)

一、使用系统自带的字体

1.在xml中修改字体


[html]  view plain  copy
  1. <!--  不指明typeface -->  
  2.             <TextView style="@style/TextStyle"  
  3.                 android:text="Hello , world  中华人民共和国(没有设置属性)"/>  
  4.   
  5.             <!--  使用默认的normal字体-->  
  6.             <TextView  
  7.                 style="@style/TextStyle"  
  8.                 android:typeface="normal"  
  9.                 android:text="Hello , world  中华人民共和国(xml设置normal)"/>  
  10.   
  11.             <!--  使用默认的sans字体-->  
  12.             <TextView  
  13.                 style="@style/TextStyle"  
  14.                 android:typeface="sans"  
  15.                 android:text="Hello , world  中华人民共和国(xml设置sans)"/>  
  16.   
  17.             <!--  使用默认的serifs字体-->  
  18.             <TextView  
  19.                 style="@style/TextStyle"  
  20.                 android:typeface="serif"  
  21.                 android:text="Hello , world  中华人民共和国(xml设置serif)"/>  
  22.   
  23.             <!--  使用默认的monospace字体-->  
  24.             <TextView  
  25.                 style="@style/TextStyle"  
  26.                 android:typeface="monospace"  
  27.                 android:text="Hello , world  中华人民共和国(xml设置monospace)"/>  


2.在Java代码中修改字体

[html]  view plain  copy
  1. vSansText = (TextView) findViewById(R.id.sans);  
  2. vSerifText = (TextView) findViewById(R.id.serif);  
  3. vMonospaceText = (TextView) findViewById(R.id.monospace);  
  4.   
  5. //设置字体样式  
  6. vSansText.setTypeface(Typeface.SANS_SERIF);  
  7. vSerifText.setTypeface(Typeface.SERIF);  
  8. vMonospaceText.setTypeface(Typeface.MONOSPACE);  

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



[html]  view plain  copy
  1. //从asset 读取字体  
  2. //得到AssetManager  
  3. AssetManager mgr = getAssets();  
  4. //根据路径得到Typeface  
  5. Typeface tf = Typeface.createFromAsset(mgr, "fonts/HelveticaNeueLTPro-UltLt.otf");  
  6. //设置字体  
  7. vTTFText.setTypeface(tf);  

  • 6
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值