`
crazier9527
  • 浏览: 996094 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

android字体(转)

阅读更多

在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. public class Test extends Activity {  
  2.     @Override  
  3.     public void onCreate(Bundle icicle) {  
  4.         super.onCreate(icicle);  
  5.         setContentView(R.layout.main);  
  6.           
  7.         TextView tv=(TextView)findViewById(R.id.custom);  
  8.         Typeface face=Typeface.createFromAsset(getAssets(),  
  9.                                                                                         "fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取  
  10.           
  11.         tv.setTypeface(face);  
  12.     }  
  13. }  

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

分享到:
评论

相关推荐

    android字体自动适应

    android字体自动适应

    android常见字体库.zip

    常见的字体库,而Android字体库,这本来是一个很大的库的,我通过字体压缩工具已经把字体库的文件缩小了很多,可以直接下载使用,不需要再用工具转换

    android 改变字体字体和大小

    android 改变字体字体和大小,单机按钮来改变字体和大小

    Android字体大小自适应不同分辨率的解决办法

    Android字体大小自适应不同分辨率的解决办法 今天有人问我,Android系统不同分辨率,不同大小的手机,字体大小怎么去适应呢?其实字体的适应和图片的适应是一个道理的。 一、原理如下: 假设需要适应320×240,480...

    android发光字体的实现

    这是android字体能够发光的特殊处理,吸引用户的眼球,简单易用。

    Android字体类库Calligraphy.zip

    Calligraphy 是一个用来简化 Android 应用使用自定义字体的类库, 该类库会自动查找应用中的 TextView 并设置其使用的字体。效果图:使用:添加依赖库(Download (.aar))dependencies {  compile 'uk.co....

    Android实现文字转图片

    Android实现文字转图片,可以设置文字大小,文字字体,文字颜色,文字透明度,背景颜色,背景透明度,是否保存到SD卡上

Global site tag (gtag.js) - Google Analytics