Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android字體設置及Roboto字體使用方法

Android字體設置及Roboto字體使用方法

編輯:Android開發實例

本文實例講述了Android字體設置及Roboto字體使用方法。分享給大家供大家參考。具體分析如下:

一、自定義字體

1.android Typeface使用TTF字體文件設置字體

我們可以在程序中放入ttf字體文件,在程序中使用Typeface設置字體。
第一步,在assets目錄下新建fonts目錄,把ttf字體文件放到這。
第二步,程序中調用:
代碼如下:
AssetManager mgr=getAssets();//得到AssetManager
Typeface tf=Typeface.createFromAsset(mgr, "fonts/ttf.ttf");//根據路徑得到Typeface
tv=findViewById(R.id.textview);
tv.setTypeface(tf);//設置字體

2.在xml文件中使用android:textStyle=”bold” 可以將英文設置成粗體, 但是不能將中文設置成粗體,
將中文設置成粗體的方法是:
代碼如下:
TextView tv = (TextView)findViewById(R.id.TextView01);  
tv.getPaint().setFakeBoldText(true);//中文仿“粗體”--使用TextPaint的仿“粗體”設置setFakeBoldText為true。

注意:部分字體中文無效,雖然不會報錯,但是對中文無效。

二、使用RoBoto

自從Android4.0後默認字體就使用了Roboto,下面介紹一下使用方法:
代碼如下:
android:fontFamily="sans-serif" // roboto regular 
android:fontFamily="sans-serif-light" // roboto light 
android:fontFamily="sans-serif-condensed" // roboto condensed 
android:fontFamily="sans-serif-thin" // roboto thin (android 4.2) 
//in combination with 
android:text
可用的參數如下:

Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic

希望本文所述對大家的Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved