Android 全局替换项目默认字体
生活随笔
收集整理的這篇文章主要介紹了
Android 全局替换项目默认字体
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Android 全局替換項目默認字體
項目中,我一開始用的是默認字體-思源黑體,最后項目都已經完成上線了,結果說要把字體改為蘋方。我不可能給每一個TextView 、Button、EditText等控件單獨去設置蘋方字體。在這里我介紹一下我用的方法。
首先、創建替換字體用到的工具類TypefaceUtil
/*** created by DELL* on 2020/11/6*/public class TypefaceUtil {/*** 為給定的字符串添加HTML紅色標記,當使用Html.fromHtml()方式顯示到TextView 的時候其將是紅色的** @param string 給定的字符串* @return*/public static String addHtmlRedFlag(String string) {return "<font color=\"red\">" + string + "</font>";}/*** 將給定的字符串中所有給定的關鍵字標紅** @param sourceString 給定的字符串* @param keyword 給定的關鍵字* @return 返回的是帶Html標簽的字符串,在使用時要通過Html.fromHtml()轉換為Spanned對象再傳遞給TextView對象*/public static String keywordMadeRed(String sourceString, String keyword) {String result = "";if (sourceString != null && !"".equals(sourceString.trim())) {if (keyword != null && !"".equals(keyword.trim())) {result = sourceString.replaceAll(keyword, "<font color=\"red\">" + keyword + "</font>");} else {result = sourceString;}}return result;}/*** <p>Replace the font of specified view and it's children</p>* @param root The root view.* @param fontPath font file path relative to 'assets' directory.*/public static void replaceFont(@NonNull View root, String fontPath) {if (root == null || TextUtils.isEmpty(fontPath)) {return;}if (root instanceof TextView) { // If view is TextView or it's subclass, replace it's fontTextView textView = (TextView)root;int style = Typeface.NORMAL;if (textView.getTypeface() != null) {style = textView.getTypeface().getStyle();}textView.setTypeface(createTypeface(root.getContext(), fontPath), style);} else if (root instanceof ViewGroup) { // If view is ViewGroup, apply this method on it's child viewsViewGroup viewGroup = (ViewGroup) root;for (int i = 0; i < viewGroup.getChildCount(); ++i) {replaceFont(viewGroup.getChildAt(i), fontPath);}}}/*** <p>Replace the font of specified view and it's children</p>* 通過遞歸批量替換某個View及其子View的字體改變Activity內部控件的字體(TextView,Button,EditText,CheckBox,RadioButton等)* @param context The view corresponding to the activity.* @param fontPath font file path relative to 'assets' directory.*/public static void replaceFont(@NonNull Activity context, String fontPath) {replaceFont(getRootView(context),fontPath);}/** Create a Typeface instance with your font file*/public static Typeface createTypeface(Context context, String fontPath) {return Typeface.createFromAsset(context.getAssets(), fontPath);}/*** 從Activity 獲取 rootView 根節點* @param context* @return 當前activity布局的根節點*/public static View getRootView(Activity context){return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);}/*** 通過改變App的系統字體替換App內部所有控件的字體(TextView,Button,EditText,CheckBox,RadioButton等)* @param context* @param fontPath* 需要修改style樣式為monospace:*/ // <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> // <!-- Customize your theme here. --> // <!-- Set system default typeface --> // <item name="android:typeface">monospace</item> // </style>public static void replaceSystemDefaultFont(@NonNull Context context, @NonNull String fontPath) {replaceTypefaceField("MONOSPACE", createTypeface(context, fontPath));}/*** <p>Replace field in class Typeface with reflection.</p>*/private static void replaceTypefaceField(String fieldName, Object value) {try {Field defaultField = Typeface.class.getDeclaredField(fieldName);defaultField.setAccessible(true);defaultField.set(null, value);} catch (NoSuchFieldException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();}} }下載字體文件、放入項目
我們下載下來字體文件,不管是.ttf還是.otf文件都行,放入下面的路徑下。記住fonts文件名不要寫錯了。
修改主題style
在我們的主題style中,添加如下代碼:monospace要與工具類中的名字一致。
<item name="android:typeface">monospace</item>最后的調用
在我們項目的appliction的oncreate方法里面調用即可。
TypefaceUtil.replaceSystemDefaultFont(this,"fonts/pingfang_medium.ttf");到此為項目字體的全局替換已經完成了。但我們要知道的是,這樣寫有一個缺點,我用的字體文件有10M,這就代表著最后的打包會增加10M。
總結
以上是生活随笔為你收集整理的Android 全局替换项目默认字体的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 多年前的树莓派 B+ 重新工作
- 下一篇: HTML代码转换编辑器