close
Android 在設定文字的字型,
除了調用內建的字型外, 也可以引用外部的字型
TextView mTextView = (TextView) findViewById(R.id.mTextView);
mTextView.setTypeface(
Typeface.createFormAsset(getAssets(), "fonts/HandmadeTypewriter.ttf")
);
必需將引用的字型檔, 放置於 fonts 的目錄底下,
透過 AssetsManage 來引用外部的資源, 即可設定該字型,
但需注意該字型必需為 TTF (True Type Font) 的格式,
否則會造成字型無法顯示、或是程式無法編譯的問題
除了以 createFromAsset 的方式來調用外部的字型外
也可以利用 defaultFromStyle 的方式來使用 Android 內建的字型
除將字型直接放入手機中, 也可以以 import 的方式來匯入專案
Resources.StyledAttributes resFont =
getContext().obtainStyledAttributes
(attrs, R.styleable.UnicodeTextView);
String fontName = resFont.getString(R.styleable.UnicodeTextView_font);
if(fontName != null) {
// 在這理處理變更字型的程式
}
全站熱搜