XML 設定 android:textStyle="bold",程式碼 setTypeface(customFont) 後粗體失效。
setTypeface(Typeface) 會**完全替換**字體樣式,XML 的 textStyle 被覆蓋。
textView.setTypeface(customFont, Typeface.BOLD); // 同時指定字體與樣式
| 常數 | 效果 |
|---|---|
Typeface.NORMAL | 正常 |
Typeface.BOLD | 粗體 |
Typeface.ITALIC | 斜體 |
Typeface.BOLD_ITALIC | 粗斜體 |
// 根據條件切換
int style = isImportant ? Typeface.BOLD : Typeface.NORMAL;
textView.setTypeface(customFont, style);
.ttf/.otf) 需自行包含粗體字形,否則系統會模擬粗體(效果較差)Font-Regular.ttf、Font-Bold.ttf 兩個檔案,分別載入