2019-04-04 Android 切換主題 (二)

Android 切換主題 (二)

背景

我原來寫過一篇文章關(guān)于 android 切換主題的文章 --?Android 切換主題以及換膚的實現(xiàn)?, 里面介紹了如何使用?setTheme()?來切換主題,只不過使用這個函數(shù)有個缺點那就是你必須得重新啟動當前的 Activity 才能生效!那么問題來了,有沒有方法保證我們既使用了?setTheme()?又能不重啟當前的 Activity 呢?我告訴這是有的,下面我就是詳細介紹這個方法 (參考 github 上的一個開源項目進行介紹!文末給出這個項目的地址)。

===

原理

眾所周知,setTheme()?之后需要重啟當前的 Activity 的原因是:它要重新渲染當前的 當前的 ViewTree 。所以現(xiàn)在我們的做法就是我們來自己給他渲染不就行了!那樣的話,就不需要重啟當前的 Activity 了!下面我們就來看代碼吧!

===

代碼

代碼的實現(xiàn)核心就是:在用戶調(diào)用?setTheme()?之后,我們獲取當前的 Theme ,之后我們在從中獲取到我們用的屬性,之后在設(shè)置到對應(yīng)的控件上,這樣就ok了!

第一步

我們先定義個接口:

publicinterfaceColorUiInterface{publicViewgetView();publicvoidsetTheme(Resources.Theme themeId);}

這樣的話我們就可以重寫所有的控件,讓他繼承該接口,并實現(xiàn)對應(yīng)的函數(shù),那么在?setTheme()?之后就可以直接調(diào)用每個控件都有的?setTheme()?方法了!

第二步

現(xiàn)在我們就來實現(xiàn)一個自定義的view

publicclassColorTextViewextendsTextViewimplementsColorUiInterface{privateintattr_drawable = -1;privateintattr_textAppearance = -1;privateintattr_textColor = -1;publicColorTextView(Context context){super(context);? ? }publicColorTextView(Context context, AttributeSet attrs){super(context, attrs);this.attr_drawable = ViewAttributeUtil.getBackgroundAttibute(attrs);this.attr_textColor = ViewAttributeUtil.getTextColorAttribute(attrs);? ? }publicColorTextView(Context context, AttributeSet attrs,intdefStyleAttr){super(context, attrs, defStyleAttr);this.attr_drawable = ViewAttributeUtil.getBackgroundAttibute(attrs);this.attr_textColor = ViewAttributeUtil.getTextColorAttribute(attrs);? ? }@OverridepublicViewgetView(){returnthis;? ? }@OverridepublicvoidsetTheme(Resources.Theme themeId){if(attr_drawable != -1) {? ? ? ? ? ? ViewAttributeUtil.applyBackgroundDrawable(this, themeId, attr_drawable);? ? ? ? }if(attr_textColor != -1) {? ? ? ? ? ? ViewAttributeUtil.applyTextColor(this, themeId, attr_textColor);? ? ? ? }? ? }}

從以上代碼中我們可以看到,我首先獲取了一下一些常用的需要換膚的要素,比如:背景色,字體顏色,當讓還有其他的,這個隨用戶定制!當然讀者也可以實現(xiàn)其他的自定義 view 。

第三步

現(xiàn)在我們來看下?ViewAttributeUtil?這個類的具體實現(xiàn)!

publicclassViewAttributeUtil{publicstaticintgetAttributeValue(AttributeSet attr,intparamInt){intvalue=-1;intcount = attr.getAttributeCount();for(inti =0; i

這個類比較簡單,就是根據(jù)對應(yīng)的 themeid 得到對應(yīng) themeid 的值!好了目前為止,我們還差一步就是,當我們調(diào)用了 Activity 的?setTheme()?方法之后,重新設(shè)置到對應(yīng)的view就行了!

第四步

直接上代碼

publicclassColorUiUtil{/** * 切換應(yīng)用主題 * *@paramrootView */publicstaticvoidchangeTheme(View rootView, Resources.Theme theme) {if(rootViewinstanceofColorUiInterface) {? ? ? ? ((ColorUiInterface) rootView).setTheme(theme);if(rootViewinstanceofViewGroup) {intcount = ((ViewGroup) rootView).getChildCount();for(inti =0; i < count; i++) {? ? ? ? ? ? ? ? changeTheme(((ViewGroup) rootView).getChildAt(i), theme);? ? ? ? ? ? }? ? ? ? }if(rootViewinstanceofAbsListView) {try{? ? ? ? ? ? ? ? Field localField = AbsListView.class.getDeclaredField("mRecycler");? ? ? ? ? ? ? ? localField.setAccessible(true);? ? ? ? ? ? ? ? Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear",newClass[0]);? ? ? ? ? ? ? ? localMethod.setAccessible(true);? ? ? ? ? ? ? ? localMethod.invoke(localField.get(rootView),newObject[0]);? ? ? ? ? ? }catch(NoSuchFieldException e1) {? ? ? ? ? ? ? ? e1.printStackTrace();? ? ? ? ? ? }catch(ClassNotFoundException e2) {? ? ? ? ? ? ? ? e2.printStackTrace();? ? ? ? ? ? }catch(NoSuchMethodException e3) {? ? ? ? ? ? ? ? e3.printStackTrace();? ? ? ? ? ? }catch(IllegalAccessException e4) {? ? ? ? ? ? ? ? e4.printStackTrace();? ? ? ? ? ? }catch(InvocationTargetException e5) {? ? ? ? ? ? ? ? e5.printStackTrace();? ? ? ? ? ? }? ? ? ? }? ? }else{if(rootViewinstanceofViewGroup) {intcount = ((ViewGroup) rootView).getChildCount();for(inti =0; i < count; i++) {? ? ? ? ? ? ? ? changeTheme(((ViewGroup) rootView).getChildAt(i), theme);? ? ? ? ? ? }? ? ? ? }if(rootViewinstanceofAbsListView) {try{? ? ? ? ? ? ? ? Field localField = AbsListView.class.getDeclaredField("mRecycler");? ? ? ? ? ? ? ? localField.setAccessible(true);? ? ? ? ? ? ? ? Method localMethod = Class.forName("android.widget.AbsListView$RecycleBin").getDeclaredMethod("clear",newClass[0]);? ? ? ? ? ? ? ? localMethod.setAccessible(true);? ? ? ? ? ? ? ? localMethod.invoke(localField.get(rootView),newObject[0]);? ? ? ? ? ? }catch(NoSuchFieldException e1) {? ? ? ? ? ? ? ? e1.printStackTrace();? ? ? ? ? ? }catch(ClassNotFoundException e2) {? ? ? ? ? ? ? ? e2.printStackTrace();? ? ? ? ? ? }catch(NoSuchMethodException e3) {? ? ? ? ? ? ? ? e3.printStackTrace();? ? ? ? ? ? }catch(IllegalAccessException e4) {? ? ? ? ? ? ? ? e4.printStackTrace();? ? ? ? ? ? }catch(InvocationTargetException e5) {? ? ? ? ? ? ? ? e5.printStackTrace();? ? ? ? ? ? }? ? ? ? }? ? }}}

代碼很簡單,就是用了遞歸,從當前的 ViewTree 中找到 繼承了我們在第一步定義的那個接口,之后再調(diào)用其?setTheme()?方法就行了??!

最后一步

那么我們現(xiàn)在來看下如何在 Activity 中是如何調(diào)用的!

setTheme(R.style.theme_1);ColorUiUtil.changeTheme(rootView,getTheme());

是不是很簡單呀!

總結(jié)

這個方法我感覺蠻好的,要說唯一的缺點吧!就是使用了遞歸!有點耗性能!但是這個我估計是可以忽略不計的?。?/p>

*** 現(xiàn)在給出這個開源項目的地址?MultipleTheme?***

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容