Android 开发使用自定义字体

有时候,系统自带的字体并不能满足我们特殊的需求,这时候就需要引用其他的字体了,可以把下载的字体文件放在 assets 目录下。

自定义字体文件不能使用xml代码读取而应该使用java代码: 

public class MyActivity extends Activity {    
    private TextView mText;

     @Override    
     public void onCreate(Bundle savedInstanceState) {    
         super.onCreate(savedInstanceState);    
         setContentView(R.layout.activity_main);    
         
         TextView tv=(TextView)findViewById(R.id.custom);    
         Typeface face=Typeface.createFromAsset(getAssets(), "comic sans ms.ttf");   
         mText.setTypeface(face);    
    }    
}   

 

PS: Android Studio 下若没有 assets 目录,可以在 main 文件夹下自建一个,如图所示:

 

主要参考:http://blog.csdn.net/qeqeqe236/article/details/7000928

 

此外,http://blog.sina.com.cn/s/blog_8a86f4dd0101j18w.html 还提到了继承 TextView 的方法,有待实验验证。

posted @ 2015-06-10 20:58  WriteOnRead  阅读(592)  评论(0编辑  收藏  举报