欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 夜間模式的實(shí)現(xiàn)代碼示例

 更新時(shí)間:2017年03月03日 09:44:17   作者:陳三哥  
本篇文章主要介紹了Android 夜間模式的實(shí)現(xiàn)代碼示例,實(shí)現(xiàn)能夠根據(jù)不同的設(shè)定,呈現(xiàn)不同風(fēng)格的界面給用戶,有興趣的可以了解一下。

夜間模式實(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)文章

最新評(píng)論