国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 夜間模式的實現代碼示例

2019-12-12 03:31:00
字體:
來源:轉載
供稿:網友

夜間模式實現

所謂的夜間模式,就是能夠根據不同的設定,呈現不同風格的界面給用戶,而且晚上看著不傷眼睛,實現方式也就是所謂的換膚(主題切換)。對于夜間模式的實現網上流傳了很多種方式。也反編譯了幾個新聞類(你懂得)夜間模式實現的比較的好的App,好歹算是實現了。方式有很多,我現在把我所實現原理(內置主題的方式)分享出來,希望能幫到大家,不喜勿噴(近來筆者小心肝不太安生),有更好的方法也歡迎分享。

實現夜間模式的時候,我一直糾結下面幾個問題

  • 從何處著手。
  • 選中夜間模式,如何才能使當前所看到的頁面立即呈現出夜間模式的效果又不閃屏
  • 其他頁面如何設置,特別是在Actionbar上的或者有側邊欄Menu的,比如使用了(actionbar――sherlock)庫。

上面的問題咱們先一個一個解決:

其一:從何處著手

1.1定義屬性

要想根據主題的不同,設置不同屬性,我們至少需要定義下屬性的名字吧。要不然系統怎么知道去哪找啊!

定義屬性,是在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文件的內容可以看到,attr里可以定義各種屬性類型,如color、float、integer、boolean、dimension(sp、dp/dip、px、pt...)、reference(指向本地資源),還有curVisibility是枚舉屬性,對應view的invisibility、visibility、gone。

1.2定義主題 

接著,我們需要在資源文件中定義若干套主題。并且在主題中設置各個屬性的值。

本例中,我在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類型、尺寸設置等)。

1.4設置主題及布局文件

布局文件與主題都寫好了,接下來我們就要在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();方法之前設置。

如果你使用的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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广南县| 泊头市| 辉县市| 长乐市| 广水市| 沭阳县| 颍上县| 昌黎县| 邵阳县| 洛阳市| 平山县| 南雄市| 赤峰市| 富阳市| 梁山县| 汉寿县| 吉安市| 文登市| 溧水县| 凌海市| 苏尼特右旗| 靖宇县| 罗江县| 长丰县| 墨江| 巴东县| 隆德县| 西林县| 宝山区| 仪陇县| 石景山区| 饶平县| 勐海县| 波密县| 元谋县| 临朐县| 昌都县| 绥棱县| 西充县| 博罗县| 仲巴县|