Android 編程下字型檔的使用

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   color   os   ar   使用   

在安卓作業系統下對於 TextView 字型的支援非常有限,預設情況下 TextView 的 typeface 屬性支援 "Sans","serif","monospace" 這三種字型,如果在沒有指定字型的情況下,系統預設會使用 "Sans" 作為文本顯示的字型。但這三種字型只支援英文,也就是說只要你顯示的文字是中文,無論你選擇這三種字型中的哪一種,顯示效果都是一樣的。為此,Google又提供了另外一種字型的支援方式:字型檔。在 Android 2.2 版本中,安卓對字型檔的支援是比較有限的,從 Android 2.3.3 版本開始,安卓系統已經能夠支援大部分的字型檔。這意味著你在 SDK 版本為 Android 2.2 的平台下寫的使用字型檔的代碼只有在 Android 2.3.3 系統及其更的高版本中才會生效;如果你將代碼運行在 Android 2.2 的系統上,你將不會看到自己所期望的效果,取而代之的是一片空白。

下面來看一下代碼實現過程,在代碼實現實現之前,我們先來看下不同字型檔的顯示效果,中第一張圖:SIMKAI.TTF 簡體字-楷體-常規、第二張圖:MSYH.TTF 簡體字-微軟雅黑-常規。

 1 package cn.sunzn.fonts; 2  3 import android.app.Activity; 4 import android.graphics.Typeface; 5 import android.os.Bundle; 6 import android.view.Menu; 7 import android.widget.TextView; 8  9 public class MainActivity extends Activity {10 11     private TextView tv_title;12     private TextView tv_author;13     private TextView tv_line1;14     private TextView tv_line2;15     private TextView tv_line3;16     private TextView tv_line4;17 18     public void onCreate(Bundle savedInstanceState) {19         super.onCreate(savedInstanceState);20         setContentView(R.layout.activity_main);21         /**22          * 在 assets 目錄下建立 fonts 檔案夾並放入要使用的字型檔(**.ttf)並提供相對路徑給23          * createFromAsset(AssetManager mgr, String path) 來建立 Typeface 對象,再通24          * 過TextView.setTypeface(Typeface tf) 指定文本顯示的字型。25          */26         Typeface face = Typeface.createFromAsset(getAssets(),"fonts/SIMFANG.TTF");27 28         tv_title = (TextView) findViewById(R.id.tv_title);29         tv_title.setTypeface(face);30 31         tv_author = (TextView) findViewById(R.id.tv_author);32         tv_author.setTypeface(face);33 34         tv_line1 = (TextView) findViewById(R.id.tv_line1);35         tv_line1.setTypeface(face);36 37         tv_line2 = (TextView) findViewById(R.id.tv_line2);38         tv_line2.setTypeface(face);39         tv_line3 = (TextView) findViewById(R.id.tv_line3);40         tv_line3.setTypeface(face);41 42         tv_line4 = (TextView) findViewById(R.id.tv_line4);43         tv_line4.setTypeface(face);44     }45 46     public boolean onCreateOptionsMenu(Menu menu) {47         getMenuInflater().inflate(R.menu.activity_main, menu);48         return true;49     }50 51 }

最後,還需要補充說明一下:雖然採用這種方式可以實現自己想要的顯示效果,但是在採用這種方法之前開發人員還是需要考慮下效能消耗和運行環境以及自己 APP 的風格,然後再做出對應的選擇。原因有以下幾點:

  • 字型檔檔案的大小:因為字型檔的尺寸少則幾兆,多則幾十兆,檔案打包的時候對 *.ttf 格式的檔案壓縮率很低,所以會無形中增大 APP 的體積。
  • 運行環境的考慮:如果你的應用面向的是高版本的運行環境,可以考慮使用這種方法,如果使用者的運行環境是 Android 2.2 需要放棄該方法。
  • 應用風格的考慮:如果你的應用既面向高版本運行環境,並且你的應用中統一使用指定的字型,可以考慮使用該方法;若是單單為了應用中的某幾個字的顯示效果就使用字型檔,則會得不償失。

Android 編程下字型檔的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.