在android系统中,自带的只有三种字体---"sans","serif"和"monospace",你只要在xml文件中的

Xml代码
  1. android:typeface
属性使用它们:
Xml代码
  1. <TableLayout
  2. xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:stretchColumns="1">
  6. <TableRow>
  7. <TextView
  8. android:text="sans:"
  9. android:layout_marginRight="4px"
  10. android:textSize="20sp"
  11. />
  12. <TextView
  13. android:id="@+id/sans"
  14. android:text="Hello,world!"
  15. android:typeface="sans"
  16. android:textSize="20sp"
  17. />
  18. </TableRow>
  19. <TableRow>
  20. <TextView
  21. android:text="serif:"
  22. android:layout_marginRight="4px"
  23. android:textSize="20sp"
  24. />
  25. <TextView
  26. android:id="@+id/serif"
  27. android:text="Hello,world!"
  28. android:typeface="serif"
  29. android:textSize="20sp"
  30. />
  31. </TableRow>
  32. <TableRow>
  33. <TextView
  34. android:text="monospace:"
  35. android:layout_marginRight="4px"
  36. android:textSize="20sp"
  37. />
  38. <TextView
  39. android:id="@+id/monospace"
  40. android:text="Hello,world!"
  41. android:typeface="monospace"
  42. android:textSize="20sp"
  43. />
  44. </TableRow>
  45. <TableRow>
  46. <TextView
  47. android:text="Custom:"
  48. android:layout_marginRight="4px"
  49. android:textSize="20sp"
  50. />
  51. <TextView
  52. android:id="@+id/custom"
  53. android:text="Hello,world!"
  54. android:textSize="20sp"
  55. />
  56. </TableRow>
  57. </TableLayout>

有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在assets目录下.自定义字体文件不能使用xml代码读取而应该使用java代码:
Java代码
  1. publicclassTestextendsActivity{
  2. @Override
  3. publicvoidonCreate(Bundleicicle){
  4. super.onCreate(icicle);
  5. setContentView(R.layout.main);
  6. TextViewtv=(TextView)findViewById(R.id.custom);
  7. Typefaceface=Typeface.createFromAsset(getAssets(),
  8. "fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取
  9. tv.setTypeface(face);
  10. }
  11. }

这个ttf文件在http://moorstation.org/typoasis/designers/klein07/text01/handmade.htm。
注意:如果你下载的字体Android不支持,那么系统会使用默认字体"sans"。由于应用自定义字体需要额外空间,所以一般不是都需要自定义字体的.

更多相关文章

  1. 箭头函数的基础使用
  2. NPM 和webpack 的基础使用
  3. Python list sort方法的具体使用
  4. 【阿里云镜像】使用阿里巴巴DNS镜像源——DNS配置教程
  5. Android下使用Logcat打印信息
  6. Android(安卓)官方Sample
  7. Dojo mobile TweetView 系列教程之五 —— TweetView: Android,打
  8. android UI布局 layer-list
  9. 关于Android资源文件中出现百分号的问题

随机推荐

  1. android开发框架汇总
  2. Android(安卓)SharedPreferences的使用
  3. android 按钮的文字显示不全
  4. 解决录音文件丢失问题
  5. android 多国语言
  6. android 让 webView 中的超链接失效
  7. android 实现由下至上弹出并位于屏幕底部
  8. Android(安卓)分享一个流量显示界面
  9. Android画虚线
  10. android 内存优化