Android 夜間模式的實(shí)現(xiàn)代碼示例
夜間模式實(shí)現(xiàn)
所謂的夜間模式,就是能夠根據(jù)不同的設(shè)定,呈現(xiàn)不同風(fēng)格的界面給用戶,而且晚上看著不傷眼睛,實(shí)現(xiàn)方式也就是所謂的換膚(主題切換)。對(duì)于夜間模式的實(shí)現(xiàn)網(wǎng)上流傳了很多種方式。也反編譯了幾個(gè)新聞?lì)悾愣茫┮归g模式實(shí)現(xiàn)的比較的好的App,好歹算是實(shí)現(xiàn)了。方式有很多,我現(xiàn)在把我所實(shí)現(xiàn)原理(內(nèi)置主題的方式)分享出來(lái),希望能幫到大家,不喜勿噴(近來(lái)筆者小心肝不太安生),有更好的方法也歡迎分享。
實(shí)現(xiàn)夜間模式的時(shí)候,我一直糾結(jié)下面幾個(gè)問(wèn)題
- 從何處著手。
- 選中夜間模式,如何才能使當(dāng)前所看到的頁(yè)面立即呈現(xiàn)出夜間模式的效果又不閃屏
- 其他頁(yè)面如何設(shè)置,特別是在Actionbar上的或者有側(cè)邊欄Menu的,比如使用了(actionbar——sherlock)庫(kù)。
上面的問(wèn)題咱們先一個(gè)一個(gè)解決:
其一:從何處著手
1.1定義屬性
要想根據(jù)主題的不同,設(shè)置不同屬性,我們至少需要定義下屬性的名字吧。要不然系統(tǒng)怎么知道去哪找??!
定義屬性,是在values下進(jìn)行。
在attrs.xml里定義了幾種屬性。
<?xml version="1.0" encoding="utf-8"?> <resources> <attr name="colorValue" format="color" /> <attr name="floatValue" format="float" /> <attr name="integerValue" format="integer" /> <attr name="booleanValue" format="boolean" /> <attr name="dimensionValue" format="dimension" /> <attr name="stringValue" format="string" /> <attr name="referenceValue" format="color|reference" /> <attr name="imageValue" format="reference"/> <attr name="curVisibility"> <enum name="show" value="0" /> <!-- Not displayed, but taken into account during layout (space is left for it). --> <enum name="inshow" value="1" /> <!-- Completely hidden, as if the view had not been added. --> <enum name="hide" value="2" /> </attr> </resources>
從上面的xml文件的內(nèi)容可以看到,attr里可以定義各種屬性類(lèi)型,如color、float、integer、boolean、dimension(sp、dp/dip、px、pt...)、reference(指向本地資源),還有curVisibility是枚舉屬性,對(duì)應(yīng)view的invisibility、visibility、gone。
1.2定義主題
接著,我們需要在資源文件中定義若干套主題。并且在主題中設(shè)置各個(gè)屬性的值。
本例中,我在styles.xml里定義了DayTheme與NightTheme。
<style name="DayTheme" parent="Theme.Sherlock.Light">> <item name="colorValue">@color/title</item> <item name="floatValue">0.35</item> <item name="integerValue">33</item> <item name="booleanValue">true</item> <item name="dimensionValue">16dp</item> <!-- 如果string類(lèi)型不是填的引用而是直接放一個(gè)字符串,在布局文件中使用正常,但代碼里獲取的就有問(wèn)題 --> <item name="stringValue">@string/action_settings</item> <item name="referenceValue">@drawable/bg</item> <item name="imageValue">@drawable/launcher_icon</item> <item name="curVisibility">show</item> </style> <style name="NightTheme" parent="Theme.Sherlock.Light"> <item name="colorValue">@color/night_title</item> <item name="floatValue">1.44</item> <item name="integerValue">55</item> <item name="booleanValue">false</item> <item name="dimensionValue">18sp</item> <item name="stringValue">@string/night_action_settings</item> <item name="referenceValue">@drawable/night_bg</item> <item name="imageValue">@drawable/night_launcher_icon</item> <item name="curVisibility">hide</item> </style>
1.3在布局文件中使用
定義好了屬性,我們接下來(lái)就要在布局文件中使用了。
為了使用主題中的屬性來(lái)配置界面,我定義了一個(gè)名為setting.xml布局文件。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/referenceValue" android:orientation="vertical" > <TextView android:id="@+id/setting_Color" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" android:textColor="?attr/colorValue" /> <CheckBox android:id="@+id/setting_show_answer_switch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="?attr/booleanValue"/> <TextView android:id="@+id/setting_Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="?attr/dimensionValue" android:text="@string/text_title" android:textColor="?attr/colorValue" /> <TextView android:id="@+id/setting_Text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="?attr/stringValue" /> <ImageView android:id="@+id/setting_Image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="?attr/imageValue" /> <View android:id="@+id/setting_line" android:layout_width="match_parent" android:layout_height="1dp" android:visibility="?attr/curVisibility" /> </LinearLayout>
從這個(gè)布局文件中可以看到,通過(guò)“?attr/……” 格式來(lái)引用主題中的值,包括(字符串、圖片、bool類(lèi)型、尺寸設(shè)置等)。
1.4設(shè)置主題及布局文件
布局文件與主題都寫(xiě)好了,接下來(lái)我們就要在Activity的onCreate方法里使用了。
大致應(yīng)該像這樣子的:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if(MyApplication.appConfig.getNightModeSwitch()){ this.setTheme(R.style.NightTheme); }else{ this.setTheme(R.style.DayTheme); } setContentView(R.layout.setting); …… }
ps:
- MyApplication.appConfig.getNightModeSwitch()//是獲取pf中當(dāng)前所處的模式。
- 一定要放到setContentView();方法之前設(shè)置。
如果你使用的fragment 大致應(yīng)該像下面的樣子:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { if(MyApplication.appConfig.getNightModeSwitch()){ getActivity().setTheme(R.style.NightTheme); }else{ getActivity().setTheme(R.style.DayTheme); } final View view = inflater.inflate(R.layout.setting, null); …… }
ps:建議放到onCreateView(……)方法里面。
值得注意的是,要是默認(rèn)主題里沒(méi)那些屬性,解析布局文件時(shí)候是會(huì)crash。這點(diǎn)在配置多個(gè)不同style時(shí)要主題時(shí),屬性可以多,但一定不能少。
比如在attrs.xml文件中
<item name="floatValue">1.44</item> <item name="integerValue">55</item>
這兩個(gè)屬性沒(méi)有用到,但卻沒(méi)有問(wèn)題。
如果按照上面的操作完畢之后,程序運(yùn)行起來(lái)應(yīng)該就會(huì)看到效果了
那第二個(gè)問(wèn)題呢?
直接看源碼
源碼地址:NightModel_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android獲取本機(jī)電話號(hào)碼的簡(jiǎn)單方法
Android獲取本機(jī)電話號(hào)碼的簡(jiǎn)單方法,需要的朋友可以參考一下2013-05-05探究Android客戶端網(wǎng)絡(luò)預(yù)連接優(yōu)化機(jī)制
一般情況下,我們都是用一些封裝好的網(wǎng)絡(luò)框架去請(qǐng)求網(wǎng)絡(luò),對(duì)底層實(shí)現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實(shí)現(xiàn),有益于我們對(duì)網(wǎng)絡(luò)加載進(jìn)行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復(fù)用機(jī)制來(lái)優(yōu)化網(wǎng)絡(luò)加載速度的原理與細(xì)節(jié)2021-06-06android使用ViewPager實(shí)現(xiàn)輪播效果
這篇文章主要為大家詳細(xì)介紹了android使用ViewPager實(shí)現(xiàn)輪播效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04Android下Button實(shí)現(xiàn)圖文混排效果
這篇文章主要為大家詳細(xì)介紹了Android下Button實(shí)現(xiàn)圖文混排效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android實(shí)現(xiàn)多張圖片合成加載動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)多張圖片合成加載動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07Android 使用mediaplayer播放res/raw文件夾中的音樂(lè)的實(shí)例
這篇文章主要介紹了Android 使用mediaplayer播放res/raw文件夾中的音樂(lè)的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-045個(gè)Android開(kāi)發(fā)中比較常見(jiàn)的內(nèi)存泄漏問(wèn)題及解決辦法
本文主要介紹了5個(gè)Android開(kāi)發(fā)中比較常見(jiàn)的內(nèi)存泄漏問(wèn)題及解決辦法,具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫(huà)
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫(huà)。那如何顯示這個(gè)效果,下面一起來(lái)看看。2016-08-08