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)置主題的方式)分享出來,希望能幫到大家,不喜勿噴(近來筆者小心肝不太安生),有更好的方法也歡迎分享。
實(shí)現(xiàn)夜間模式的時(shí)候,我一直糾結(jié)下面幾個(gè)問題
- 從何處著手。
- 選中夜間模式,如何才能使當(dāng)前所看到的頁面立即呈現(xiàn)出夜間模式的效果又不閃屏
- 其他頁面如何設(shè)置,特別是在Actionbar上的或者有側(cè)邊欄Menu的,比如使用了(actionbar——sherlock)庫。
上面的問題咱們先一個(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里可以定義各種屬性類型,如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類型不是填的引用而是直接放一個(gè)字符串,在布局文件中使用正常,但代碼里獲取的就有問題 --> <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在布局文件中使用
定義好了屬性,我們接下來就要在布局文件中使用了。
為了使用主題中的屬性來配置界面,我定義了一個(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è)布局文件中可以看到,通過“?attr/……” 格式來引用主題中的值,包括(字符串、圖片、bool類型、尺寸設(shè)置等)。
1.4設(shè)置主題及布局文件
布局文件與主題都寫好了,接下來我們就要在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)主題里沒那些屬性,解析布局文件時(shí)候是會(huì)crash。這點(diǎn)在配置多個(gè)不同style時(shí)要主題時(shí),屬性可以多,但一定不能少。
比如在attrs.xml文件中
<item name="floatValue">1.44</item> <item name="integerValue">55</item>
這兩個(gè)屬性沒有用到,但卻沒有問題。
如果按照上面的操作完畢之后,程序運(yùn)行起來應(yīng)該就會(huì)看到效果了
那第二個(gè)問題呢?
直接看源碼
源碼地址:NightModel_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來,實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
Android 動(dòng)畫之ScaleAnimation應(yīng)用詳解
本節(jié)講解ScaleAnimation 動(dòng)畫在應(yīng)用中的實(shí)現(xiàn),有需要的朋友可以參考下2012-12-12
Flutter實(shí)現(xiàn)增強(qiáng)版的頁面懸浮按鈕的示例代碼
Flutter?自帶的?FloatingActionButton?為我們提供了一個(gè)懸浮在頂部的按鈕,這個(gè)按鈕始終在最頂層,因此可以做一些快捷的操作。本文就來和大家詳細(xì)聊聊2023-01-01
Android實(shí)現(xiàn)截圖和分享功能的代碼
截圖和分享功能大家都玩過,下面通過本文給大家介紹Android實(shí)現(xiàn)截圖和分享功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的的朋友參考下吧2017-07-07
Android實(shí)現(xiàn)二級(jí)列表購物車功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)列表購物車功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10
Android實(shí)現(xiàn)按鈕點(diǎn)擊效果
本文主要介紹了Android實(shí)現(xiàn)按鈕點(diǎn)擊效果:第一次點(diǎn)擊變色,第二次恢復(fù)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能
這篇文章主要介紹了Android使用Sensor感應(yīng)器實(shí)現(xiàn)線程中刷新UI創(chuàng)建android測(cè)力計(jì)的功能,實(shí)例分析了Android使用Sensor感應(yīng)器實(shí)現(xiàn)UI刷新及創(chuàng)建測(cè)力器的技巧,需要的朋友可以參考下2015-12-12
Android實(shí)現(xiàn)滑動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)滑動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街)
這篇文章主要介紹了Android TextSwitcher實(shí)現(xiàn)文字上下翻牌效果(銅板街),需要的朋友可以參考下2017-05-05
OnSharedPreferenceChangeListener詳解及出現(xiàn)不觸發(fā)解決辦法
本文主要介紹 Android OnSharedPreferenceChangeListener的知識(shí),在Android應(yīng)用開發(fā)過程中會(huì)遇到監(jiān)聽器不觸發(fā)事件問題,這里介紹了相應(yīng)的解決辦法2016-08-08

