让我们一起爱米兰
站内搜搜:
移动设备
请扫描二维码
或访问
m.milan100.com
您所在的位置 -> 米兰百分百 -> android相关 -> Android 自定义字体

Android 自定义字体

点击数:1710 发表时间:2011-03-29 15:16:01 作者: 来源链接:
分享到:
分享到微信

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

android:typeface 

android:typeface属性使用它们:

<TableLayout 
    xmlns:android="
http://schemas.android.com/apk/res/android
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="1"> 
    <TableRow> 
        <TextView 
            android:text="sans:" 
            android:layout_marginRight="4px" 
            android:textSize="20sp" 
        /> 
        <TextView 
            android:id="@+id/sans" 
            android:text="Hello, world!" 
            android:typeface="sans" 
            android:textSize="20sp" 
        /> 
    </TableRow> 
    <TableRow> 
        <TextView 
            android:text="serif:" 
            android:layout_marginRight="4px" 
            android:textSize="20sp" 
        /> 
        <TextView 
            android:id="@+id/serif" 
            android:text="Hello, world!" 
            android:typeface="serif" 
            android:textSize="20sp" 
        /> 
    </TableRow> 
    <TableRow> 
        <TextView 
            android:text="monospace:" 
            android:layout_marginRight="4px" 
            android:textSize="20sp" 
        /> 
        <TextView 
            android:id="@+id/monospace" 
            android:text="Hello, world!" 
            android:typeface="monospace" 
            android:textSize="20sp" 
        /> 
    </TableRow> 
    <TableRow> 
        <TextView 
            android:text="Custom:" 
            android:layout_marginRight="4px" 
            android:textSize="20sp" 
        /> 
        <TextView 
            android:id="@+id/custom" 
            android:text="Hello, world!" 
            android:textSize="20sp" 
        /> 
    </TableRow> 
</TableLayout> 

<TableLayout
 xmlns:android="
http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:stretchColumns="1">
 <TableRow>
  <TextView
   android:text="sans:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/sans"
   android:text="Hello, world!"
   android:typeface="sans"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="serif:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/serif"
   android:text="Hello, world!"
   android:typeface="serif"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="monospace:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/monospace"
   android:text="Hello, world!"
   android:typeface="monospace"
   android:textSize="20sp"
  />
 </TableRow>
 <TableRow>
  <TextView
   android:text="Custom:"
   android:layout_marginRight="4px"
   android:textSize="20sp"
  />
  <TextView
   android:id="@+id/custom"
   android:text="Hello, world!"
   android:textSize="20sp"
  />
 </TableRow>
</TableLayout>

    有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在assets目录下.自定义字体文件不能使用xml代码读取而应该使用java代码:


public class Test extends Activity {  
    @Override 
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  
        setContentView(R.layout.main);  
          
        TextView tv=(TextView)findViewById(R.id.custom);  
        Typeface face=Typeface.createFromAsset(getAssets(),  
                                                                                        "fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取  
          
        tv.setTypeface(face);  
    }  

public class Test extends Activity {
 @Override
 public void onCreate(Bundle icicle) {
  super.onCreate(icicle);
  setContentView(R.layout.main);
  
  TextView tv=(TextView)findViewById(R.id.custom);
  Typeface face=Typeface.createFromAsset(getAssets(),
                      "fonts/HandmadeTypewriter.ttf");//也可以使用其他的静态方法获取
  
  tv.setTypeface(face);
 }
}

0
很 好
0
一 般
2
差 劲
热门新闻
相关文章
上一篇: webView 点击连接如何不让跳转到系统的 浏览器
下一篇: adb工具的使用
评论区
匿名

返回首页 | 收藏本页 | 回到顶部
Copyright 2010. 米兰百分百 Powered By Bridge.
京ICP备15050557号