本文執行個體講述了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:textStyle="normal|bold|italic"
可用的參數如下:
Regular
Italic
Bold
Bold-italic
Light
Light-italic
Thin
Thin-italic
Condensed regular
Condensed italic
Condensed bold
Condensed bold-italic
希望本文所述對大家的Android程式設計有所協助。