Android 設定字型
阿新 • • 發佈:2018-11-09
效果圖
實現程式碼:
1、先下載字型檔案.ttf
下載連結:http://font.chinaz.com/maobiziti.html
2、main資料夾下建立fonts資料夾,.ttf檔案複製到fonts資料夾下
3、讀取ttf檔案
private void getFontFromAssets() { mTypefaceList.add(Typeface.DEFAULT); String[] fontNameList = null; AssetManager assetManager = getAssets(); try { fontNameList = assetManager.list("fonts"); } catch (IOException e) { e.printStackTrace(); } for (int i = 0; i < fontNameList.length; i++) { String fontPath = "fonts" + "/" + fontNameList[i]; Typeface typeface = Typeface.createFromAsset(assetManager, fontPath);//根據路徑得到Typeface mTypefaceList.add(typeface); } }
4、設定文字字型:
textView.setTypeface(mTypefaceList.get(1)); textView2.setTypeface(mTypefaceList.get(2)); textView3.setTypeface(mTypefaceList.get(3)); textView4.setTypeface(mTypefaceList.get(4));