TextView的自定義字型

2021-07-07 07:23:10 字數 1255 閱讀 3499

這裡有兩種實現方法:

(1)這種實現方法比較簡單。但是只能設定個別的字型,結果可能不是你想要的。

winp.settext("typeface.default_bold");

winp.settypeface(typeface.default_bold);

default_bold是系統自帶,當然系統自帶的也有好幾種,你可以選擇自己需要的。

(2)這種實現方法稍稍複雜,但結果是不錯的。

可以使用settypeface(typeface)方法來設定文字框內文字的字型,而android的typeface又使用ttf字型檔案來設定字型。

所以,我們可以在程式中放入ttf字型檔案,在程式中使用typeface來設定字型:

第一步,在assets目錄下新建fonts目錄,把ttf字型檔案放到這裡。

第二步,程式中呼叫:

final sharedpreferences prefs =preferencemanager.getdefaultsharedpreferences(getactivity());

wind = (textview) view.findviewbyid(r.id.wind);

wind.settext(prefs.getstring("wind", ""));

assetmanager mgr1=wind.getcontext().getassets();

wind.settypeface(typeface.createfromasset(mgr1, "font/fzstk.ttf"));

看到上面的getactivity()方法,所以這個適用於fragment中。 

那麼如果在activty中,我們該怎麼寫呢?

**如下:

// 得到textview控制項物件

textview textview = (textview) findviewbyid(r.id.custom);

// 將字型檔案儲存在assets/fonts/目錄下,www.linuxidc.com建立typeface物件

typeface typeface = typeface.createfromasset(getassets(),"font/stxingka.ttf");

// 應用字型

textview.settypeface(typeface);

在這裡可以實現你想要的任何字型。效果甚好!

Android 自定義TextView字型!

兩種方法 方法一 在assets目錄下新建目錄fonts,然後存放自己的字型庫,我這裡是微軟雅黑weiruanyahei.ttf。textview tv typeface tf2 typeface.createfromasset getassets fonts weiruanyahei.ttf tv...

自定義TextView匯入外部特殊字型

先是自定義乙個textview為了接下來存放外部特殊字型,檔案目錄如下 customtextview類檔案 package com.example.administrator.customview import android.content.context import android.util....

自定義控制項textview

自定義控制項名稱 public class cheyouquanlinearlayout extends viewgroup public cheyouquanlinearlayout context context,int horizontalspacing,int verticalspacing...