日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android 全局替换项目默认字体

發(fā)布時(shí)間:2023/12/9 Android 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android 全局替换项目默认字体 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Android 全局替換項(xiàng)目默認(rèn)字體

項(xiàng)目中,我一開(kāi)始用的是默認(rèn)字體-思源黑體,最后項(xiàng)目都已經(jīng)完成上線了,結(jié)果說(shuō)要把字體改為蘋(píng)方。我不可能給每一個(gè)TextView 、Button、EditText等控件單獨(dú)去設(shè)置蘋(píng)方字體。在這里我介紹一下我用的方法。

首先、創(chuàng)建替換字體用到的工具類(lèi)TypefaceUtil
/*** created by DELL* on 2020/11/6*/public class TypefaceUtil {/*** 為給定的字符串添加HTML紅色標(biāo)記,當(dāng)使用Html.fromHtml()方式顯示到TextView 的時(shí)候其將是紅色的** @param string 給定的字符串* @return*/public static String addHtmlRedFlag(String string) {return "<font color=\"red\">" + string + "</font>";}/*** 將給定的字符串中所有給定的關(guān)鍵字標(biāo)紅** @param sourceString 給定的字符串* @param keyword 給定的關(guān)鍵字* @return 返回的是帶Html標(biāo)簽的字符串,在使用時(shí)要通過(guò)Html.fromHtml()轉(zhuǎn)換為Spanned對(duì)象再傳遞給TextView對(duì)象*/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>* 通過(guò)遞歸批量替換某個(gè)View及其子View的字體改變Activity內(nèi)部控件的字體(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 根節(jié)點(diǎn)* @param context* @return 當(dāng)前activity布局的根節(jié)點(diǎn)*/public static View getRootView(Activity context){return ((ViewGroup)context.findViewById(android.R.id.content)).getChildAt(0);}/*** 通過(guò)改變App的系統(tǒng)字體替換App內(nèi)部所有控件的字體(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();}} }
下載字體文件、放入項(xiàng)目

我們下載下來(lái)字體文件,不管是.ttf還是.otf文件都行,放入下面的路徑下。記住fonts文件名不要寫(xiě)錯(cuò)了。

修改主題style



在我們的主題style中,添加如下代碼:monospace要與工具類(lèi)中的名字一致。

<item name="android:typeface">monospace</item>

最后的調(diào)用

在我們項(xiàng)目的appliction的oncreate方法里面調(diào)用即可。

TypefaceUtil.replaceSystemDefaultFont(this,"fonts/pingfang_medium.ttf");

到此為項(xiàng)目字體的全局替換已經(jīng)完成了。但我們要知道的是,這樣寫(xiě)有一個(gè)缺點(diǎn),我用的字體文件有10M,這就代表著最后的打包會(huì)增加10M。

總結(jié)

以上是生活随笔為你收集整理的Android 全局替换项目默认字体的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。