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

Android實現(xiàn)夜間模式切換功能實現(xiàn)代碼

 更新時間:2017年03月06日 14:16:15   作者:shineflowers  
現(xiàn)在很多App都有夜間模式,特別是閱讀類的App,夜間模式現(xiàn)在已經(jīng)是閱讀類App的標配,本篇文章主要介紹了Android實現(xiàn)夜間模式功能實現(xiàn)代碼,有興趣的可以了解一下。

現(xiàn)在很多App都有夜間模式,特別是閱讀類的App,夜間模式現(xiàn)在已經(jīng)是閱讀類App的標配了,事實上,日間模式與夜間模式就是給App定義并應用兩套不同顏色的主題,用戶可以自動或者手動的開啟,今天用Android自帶的support包來實現(xiàn)夜間模式。由于Support Library在23.2.0的版本中才添加了Theme.AppCompat.DayNight主題,所以依賴的版本必須是高于23.2.0的,并且,這個特性支持的最低SDK版本為14,所以,需要兼容Android 4.0的設備,是不能使用這個特性的,在API Level 14以下的設備會默認使用亮色主題。不過現(xiàn)在4.0以下的設備應該比較少了吧,畢竟微信的minSdkVersion都設置為14了。

添加依賴

準備資源

讓應用繼承DayNight主題

<resources> 
 
 <!-- Base application theme. --> 
 <style name="AppTheme" parent="Theme.AppCompat.DayNight.DarkActionBar"> 
  <!-- Customize your theme here. --> 
  <item name="colorPrimary">@color/colorPrimary</item> 
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item> 
  <item name="colorAccent">@color/colorAccent</item> 
 </style> 
 
</resources> 

新建夜間模式資源文件夾:

在res目錄下新建values-night文件夾,然后在此目錄下新建colors.xml文件在夜間模式下的應用的資源。當然也可以根據(jù)需要新建drawable-night,layout-night等后綴為-night的夜間資源文件夾。如下:

內(nèi)容如下:

values/colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- day values colors.xml --> 
<resources> 
 <color name="colorPrimary">#009688</color> 
 <color name="colorPrimaryDark">#00796B</color> 
 <color name="colorAccent">#009688</color> 
 <color name="textColorPrimary">#616161</color> 
 <color name="viewBackground">@android:color/white</color> 
 <color name="colorDayNightChange">@android:color/holo_orange_dark</color> 
</resources> 

values/strings.xml

<resources> 
 <string name="app_name">DayNight</string> 
 <string name="day_night_label">日間模式</string> 
</resources> 

values-night/colors.xml

<?xml version="1.0" encoding="utf-8"?> 
<!-- night values colors.xml --> 
<resources> 
 <color name="colorPrimary">#35464e</color> 
 <color name="colorPrimaryDark">#212a2f</color> 
 <color name="colorAccent">#212a2f</color> 
 <color name="textColorPrimary">#616161</color> 
 <color name="viewBackground">#212a2f</color> 
 <color name="colorDayNightChange">@android:color/holo_blue_dark</color> 
</resources> 

values-night/strings.xml

<resources> 
 <string name="app_name">DayNight</string> 
 <string name="day_night_label">夜間模式</string> 
</resources> 

使Activity繼承自AppCompatActivity

在Application中設置初始主題

動態(tài)切換

代碼邏輯實現(xiàn)如下:

acitivity_main.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:layout_marginLeft="10dp" 
 android:layout_marginRight="10dp"> 
 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:gravity="center" 
  android:text="@string/day_night_label" 
  android:textSize="20sp" 
  android:textColor="@color/colorDayNightChange" /> 
 
 <Button 
  android:id="@+id/day_night_change" 
  android:layout_width="0dp" 
  android:layout_height="wrap_content" 
  android:layout_weight="1" 
  android:layout_marginLeft="5dp" 
  android:text="日夜間模式切換" 
  android:textSize="20sp" 
  android:textColor="@color/colorDayNightChange"/> 
</LinearLayout> 

MainActivity.java

package com.jackie.daynight; 
 
import android.content.res.Configuration; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.app.AppCompatDelegate; 
import android.view.View; 
import android.widget.Button; 
 
public class MainActivity extends AppCompatActivity { 
 private Button mDayNightChange; 
 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.activity_main); 
 
  mDayNightChange = (Button) findViewById(R.id.day_night_change); 
 
  mDayNightChange.setOnClickListener(new View.OnClickListener() { 
   @Override 
   public void onClick(View v) { 
    int mode = getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; 
    if (mode == Configuration.UI_MODE_NIGHT_YES) { 
     getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_NO); 
    } else if (mode == Configuration.UI_MODE_NIGHT_NO) { 
     getDelegate().setLocalNightMode(AppCompatDelegate.MODE_NIGHT_YES); 
    } 
 
    recreate(); 
   } 
  }); 
 } 
} 

MyApplication.java

package com.jackie.daynight; 
 
import android.app.Application; 
import android.support.v7.app.AppCompatDelegate; 
 
/** 
 * Created by Jackie on 2017/3/6. 
 * Application 
 */ 
 
public class MyApplication extends Application { 
 @Override 
 public void onCreate() { 
  super.onCreate(); 
 
  /** 
   * 默認設置一直使用夜間模式 
   * 
   * 這里AppCompatDelegate.setDefaultNightMode()方法可以接受的參數(shù)值有4個: 
   * MODE_NIGHT_NO. Always use the day (light) theme(一直應用日間(light)主題). 
   * MODE_NIGHT_YES. Always use the night (dark) theme(一直使用夜間(dark)主題). 
   * MODE_NIGHT_AUTO. Changes between day/night based on the time of day(根據(jù)當前時間在day/night主題間切換). 
   * MODE_NIGHT_FOLLOW_SYSTEM(默認選項). This setting follows the system's setting, which is essentially MODE_NIGHT_NO(跟隨系統(tǒng),通常為MODE_NIGHT_NO). 
   */ 
  AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES); 
 } 
} 

效果如下:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論