這裡粘貼一個activity的代碼,其中展示了Typeface的具體用法。
package cn.com.chenzheng_java;</p><p>import android.app.Activity;<br />import android.graphics.Typeface;<br />import android.os.Bundle;<br />import android.widget.TextView;<br />/**<br /> * @description 有關字型設定<br /> * @author chenzheng_java<br /> *<br /> */<br />public class FontFaceActivity extends Activity {</p><p>@Override<br />protected void onCreate(Bundle savedInstanceState) {<br />super.onCreate(savedInstanceState);<br />setContentView(R.layout.fontface);</p><p>TextView textView = (TextView)findViewById(R.id.textView_fontface);<br />/**<br /> * setTypeface(Typeface tf)設定字型<br /> * setTypeface(Typeface tf, int style)設定字型,但是其中依舊還有一些傾斜度或者加粗等設定,則通過style進行設定<br /> */<br />/*textView.setTypeface(Typeface.DEFAULT_BOLD,Typeface.ITALIC);<br />textView.setText("中國人!");*/</p><p>Typeface tf = Typeface.createFromAsset(getAssets(), "font/ARIAL.TTF");<br />textView.setTypeface(tf,Typeface.BOLD_ITALIC);<br />textView.setText("中國人!字型設定則樣!");</p><p>}<br />}<br />
-------------------------------------------------------------------------------
android.graphics.Typeface類指定了字型的style屬性,該類可以協助我們再繪製字型的時候設定字型的具體屬性,例如like textSize, textSkewX, textScaleX 等。
建立Typeface的方法有如下幾個:
我們代碼中使用的為createFromAsset()方法,該方法可以擷取我們應用中的asset檔案夾下的ttf格式的字型檔,並且根據該檔案返回一個Typeface對象。
這裡一定要注意,path並不是以/開頭的,筆者測試的時候唄困擾了很久!如果系統並不能使用我們的字型,那麼會自動使用預設字型代替。
這裡系統為我們提供了一些預設的字型,
因為很多字型並不支援BOLD/ITALIC等樣式,所以我們有的時候需要顯示的指定,這也是很多create方法中style的作用。
四種取值,為BOLD/ITALIC/NORMAL/BOLD_ITALIC
我們可以通過下面的幾個方法對當前typeface的style進行判斷: