閱讀211 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android 自定義字體(引入外部字體)

做Android開發的時候,一些軟件會要求一些特殊字體,我們需要引入外部的ttf格式的字體到程序中,具體操作步驟為:

在安卓應用程序的目錄assets中新建fonts目錄,將我們需要使用的ttf字體文件複製進去,然後代碼:

// 得到TextView控件對象
TextView textView = (TextView) findViewById(R.id.custom);
// 將字體文件保存在assets/fonts/目錄下,www.linuxidc.com創建Typeface對象
Typeface typeFace = Typeface.createFromAsset(getAssets(),"fonts/DroidSansThai.ttf");
// 應用字體
textView.setTypeface(typeFace);
如果想整個頁麵都使用同樣的字體,可以使用批處理,新增一個Java類,如下:

public class FontManager {  
    
    public static void changeFonts(ViewGroup root, Activity act) {  
    
       Typeface tf = Typeface.createFromAsset(act.getAssets(),  
              "fonts/xxx.ttf");  
    
       for (int i = 0; i < root.getChildCount(); i++) {  
           View v = root.getChildAt(i);  
           if (v instanceof TextView) {  
              ((TextView) v).setTypeface(tf);  
           else if (v instanceof Button) {  
              ((Button) v).setTypeface(tf);  
           else if (v instanceof EditText) {  
              ((EditText) v).setTypeface(tf);  
           else if (v instanceof ViewGroup) {  
              changeFonts((ViewGroup) v, act);  
           }  
       }  
    
    }  





最後更新:2017-04-02 16:48:06

  上一篇:go Oracle日期顯示問題以及trunc方法的使用
  下一篇:go Oracle日期顯示問題以及trunc方法的使用