Android 中文字型的設定方法和提示

來源:互聯網
上載者:User

標籤:init   font   stack   htm   load   字串   androi   異常   中文字型   

Android TextView字型顏色等樣式具體解釋串連:http://blog.csdn.net/pcaxb/article/details/47341249

1.使用字型庫(自己定義字型的使用):當然假設字型庫和手機的不相容擷取什麼的,可能你的中文是無效的

(1)在assets中建立檔案夾fonts,把ttf字型庫考到檔案夾下

(2)使用代碼

TextView view1 = (TextView) findViewById(R.id.tv1);Typeface tf1 = Typeface.createFromAsset(getAssets(), "fonts/DroidSerif-Bold.ttf");
view1.setTypeface(tf1);

(3)使用字型庫的問題和解決

問題:假設你的字型庫太大(xM),就會拋java.lang.RuntimeException: native typeface cannot be made

異常。當然拋這個異常不一定是字型庫大了。

解決:

用到的軟體
(a)FontCreator字型檔瀏覽軟體
(b)FontSubset大字型檔化小字型檔軟體
工具:http://download.csdn.net/detail/pcaxb/8972239

操作使用

(a)開啟FontSubset,執行FontSubsetGUI.exe
(b)填上資料
source font 是你的當前要切割的字型包
new font 是你要切割後的字型
char list 能夠選你當前切割的字型包
encodeing 選utf-8

點擊 process

字型庫的下載連結(這裡的字型庫是我用FontSubset壓縮了的,很小):http://download.csdn.net/detail/pcaxb/8974239


(4)Android字型系統庫(瞭解)

Android System Fonts(/system/fonts):
AndroidClock.ttf
AndroidClock_Highlight.ttf
AndroidClock_Solid.ttf
AndroidEmoji.ttf
Clockopia.ttf
DroidNaskh-Regular.ttf
DroidNaskhUI-Regular.ttf
DroidSans-Bold.ttf
DroidSans.ttf
DroidSansArmenian.ttf
DroidSansEthiopic-Regular.ttf
DroidSansFallback.ttf
DroidSansGeorgian.ttf
DroidSansHebrew-Bold.ttf
DroidSansHebrew-Regular.ttf
DroidSansMono.ttf
DroidSerif-Bold.ttf
DroidSerif-BoldItalic.ttf
DroidSerif-Italic.ttf
DroidSerif-Regular.ttf
MTLmr3m.ttf
Roboto-Bold.ttf
Roboto-BoldItalic.ttf
Roboto-Italic.ttf
Roboto-Light.ttf
Roboto-LightItalic.ttf
Roboto-Regular.ttf
Roboto-Thin.ttf
Roboto-ThinItalic.ttf
RobotoCondensed-Bold.ttf
RobotoCondensed-BoldItalic.ttf
RobotoCondensed-Italic.ttf
RobotoCondensed-Regular.ttf

2.設定字型的相關屬性

(1)android:fontFamily的使用方法(有較高版本號碼的限制)

相關屬性:sans-serif、sans-serif-light、sans-serif-condensed、sans-serif-thin、

(2)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:textStyle="bold"能夠將英文設定成粗體, 可是不能將中文設定成粗體。


代碼設定

TextView tv = (TextView)findViewById(R.id.TextView01);tv.getPaint().setFakeBoldText(true);//使用TextPaint的仿粗體設定setFakeBoldText為true

(3)android:fontFamily與 android:typeface

相關屬性:normal、sans、serif、monospace
參考連結:
http://developer.android.com/reference/android/widget/TextView.html#attr_android%3atypeface
http://stackoverflow.com/questions/12128331/how-to-change-fontfamily-of-textview-in-android

3.通過HTML標籤設定字型(注意有些標籤是沒有效果的):

Button btn = (Button) findViewById(R.id.bt);String source =  "這僅僅是一個測試字串,測試<b>黑體字</b>、<i>斜體字</i>、<u>底線</u>、<font color=‘red‘ face=‘微軟雅黑‘>紅色字</font>的顯示。<font face=‘幼圓‘ size=‘1‘ color=‘green‘>幼圓一號綠色字型</font>" +        "<font face=‘宋體‘>字型</font>" +        "<font face=‘微軟雅黑‘>字型</font>" +        "<font face=‘宋體‘>nishiwode</font>" +        "<font face=‘微軟雅黑‘>nishiwode</font>";btn.setText(Html.fromHtml(source));        btn.setOnFocusChangeListener(this);

4.通過以下這樣的方式的沒有什麼效果

Typeface tf1 = Typeface.create("宋體", Typeface.NORMAL);Typeface tf1 = Typeface.create("sans-serif", Typeface.NORMAL);
       

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.