Android 夜間模式的實現(xiàn)代碼示例
夜間模式實現(xiàn)
所謂的夜間模式,就是能夠根據(jù)不同的設(shè)定,呈現(xiàn)不同風格的界面給用戶,而且晚上看著不傷眼睛,實現(xiàn)方式也就是所謂的換膚(主題切換)。對于夜間模式的實現(xiàn)網(wǎng)上流傳了很多種方式。也反編譯了幾個新聞類(你懂得)夜間模式實現(xiàn)的比較的好的App,好歹算是實現(xiàn)了。方式有很多,我現(xiàn)在把我所實現(xiàn)原理(內(nèi)置主題的方式)分享出來,希望能幫到大家,不喜勿噴(近來筆者小心肝不太安生),有更好的方法也歡迎分享。
實現(xiàn)夜間模式的時候,我一直糾結(jié)下面幾個問題
- 從何處著手。
- 選中夜間模式,如何才能使當前所看到的頁面立即呈現(xiàn)出夜間模式的效果又不閃屏
- 其他頁面如何設(shè)置,特別是在Actionbar上的或者有側(cè)邊欄Menu的,比如使用了(actionbar——sherlock)庫。
上面的問題咱們先一個一個解決:
其一:從何處著手
1.1定義屬性
要想根據(jù)主題的不同,設(shè)置不同屬性,我們至少需要定義下屬性的名字吧。要不然系統(tǒng)怎么知道去哪找??!
定義屬性,是在values下進行。
在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里可以定義各種屬性類型,如color、float、integer、boolean、dimension(sp、dp/dip、px、pt...)、reference(指向本地資源),還有curVisibility是枚舉屬性,對應view的invisibility、visibility、gone。
1.2定義主題
接著,我們需要在資源文件中定義若干套主題。并且在主題中設(shè)置各個屬性的值。
本例中,我在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類型不是填的引用而是直接放一個字符串,在布局文件中使用正常,但代碼里獲取的就有問題 --> <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在布局文件中使用
定義好了屬性,我們接下來就要在布局文件中使用了。
為了使用主題中的屬性來配置界面,我定義了一個名為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>
從這個布局文件中可以看到,通過“?attr/……” 格式來引用主題中的值,包括(字符串、圖片、bool類型、尺寸設(shè)置等)。
1.4設(shè)置主題及布局文件
布局文件與主題都寫好了,接下來我們就要在Activity的onCreate方法里使用了。
大致應該像這樣子的:
@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中當前所處的模式。
- 一定要放到setContentView();方法之前設(shè)置。
如果你使用的fragment 大致應該像下面的樣子:
@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(……)方法里面。
值得注意的是,要是默認主題里沒那些屬性,解析布局文件時候是會crash。這點在配置多個不同style時要主題時,屬性可以多,但一定不能少。
比如在attrs.xml文件中
<item name="floatValue">1.44</item> <item name="integerValue">55</item>
這兩個屬性沒有用到,但卻沒有問題。
如果按照上面的操作完畢之后,程序運行起來應該就會看到效果了
那第二個問題呢?
直接看源碼
源碼地址:NightModel_jb51.rar
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
探究Android客戶端網(wǎng)絡預連接優(yōu)化機制
一般情況下,我們都是用一些封裝好的網(wǎng)絡框架去請求網(wǎng)絡,對底層實現(xiàn)不甚關(guān)注,而大部分情況下也不需要特別關(guān)注處理。了解底層的一些實現(xiàn),有益于我們對網(wǎng)絡加載進行優(yōu)化。本文就是關(guān)于根據(jù)http的連接復用機制來優(yōu)化網(wǎng)絡加載速度的原理與細節(jié)2021-06-06android使用ViewPager實現(xiàn)輪播效果
這篇文章主要為大家詳細介紹了android使用ViewPager實現(xiàn)輪播效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04Android 使用mediaplayer播放res/raw文件夾中的音樂的實例
這篇文章主要介紹了Android 使用mediaplayer播放res/raw文件夾中的音樂的實例的相關(guān)資料,需要的朋友可以參考下2017-04-045個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法
本文主要介紹了5個Android開發(fā)中比較常見的內(nèi)存泄漏問題及解決辦法,具有很好的參考價值,下面跟著小編一起來看下吧2017-02-02利用Android中的TextView實現(xiàn)逐字顯示動畫
在安卓程序啟動的時候,想逐字顯示一段話,每個字都有一個從透明到不透明的漸變動畫。那如何顯示這個效果,下面一起來看看。2016-08-08