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

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

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

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

添加依賴

準(zhǔn)備資源

讓應(yīng)用繼承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文件在夜間模式下的應(yīng)用的資源。當(dāng)然也可以根據(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中設(shè)置初始主題

動(dòng)態(tài)切換

代碼邏輯實(shí)現(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(); 
 
  /** 
   * 默認(rèn)設(shè)置一直使用夜間模式 
   * 
   * 這里AppCompatDelegate.setDefaultNightMode()方法可以接受的參數(shù)值有4個(gè): 
   * MODE_NIGHT_NO. Always use the day (light) theme(一直應(yīng)用日間(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ù)當(dāng)前時(shí)間在day/night主題間切換). 
   * MODE_NIGHT_FOLLOW_SYSTEM(默認(rèn)選項(xiàng)). 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)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android Studio配置國內(nèi)鏡像源(利用hosts)

    Android Studio配置國內(nèi)鏡像源(利用hosts)

    這篇文章主要介紹了Android Studio配置國內(nèi)鏡像源(利用hosts),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)

    Android利用Intent實(shí)現(xiàn)記事本功能(NotePad)

    這篇文章主要為大家詳細(xì)介紹了Android利用Intent實(shí)現(xiàn)簡單記事本功能(NotePad)的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Android?性能優(yōu)化實(shí)現(xiàn)全量編譯提速的黑科技

    Android?性能優(yōu)化實(shí)現(xiàn)全量編譯提速的黑科技

    這篇文章主要為大家介紹了Android?性能優(yōu)化實(shí)現(xiàn)全量編譯提速的黑科技,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-09-09
  • Android Compose衰減動(dòng)畫Animatable使用詳解

    Android Compose衰減動(dòng)畫Animatable使用詳解

    這篇文章主要為大家介紹了Android Compose衰減動(dòng)畫Animatable使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-11-11
  • proguar在Android混淆中的用法

    proguar在Android混淆中的用法

    這篇文章主要介紹了proguar在Android混淆中的用法,代碼很精簡,直達(dá)大家參考一下。
    2017-11-11
  • android listview優(yōu)化幾種寫法詳細(xì)介紹

    android listview優(yōu)化幾種寫法詳細(xì)介紹

    這篇文章只是總結(jié)下getView里面優(yōu)化視圖的幾種寫法,需要的朋友可以參考下
    2012-11-11
  • Android入門之ListView應(yīng)用解析(一)

    Android入門之ListView應(yīng)用解析(一)

    這篇文章主要介紹了Android入門之ListView應(yīng)用,簡單說明了ListView的實(shí)現(xiàn),需要的朋友可以參考下
    2014-08-08
  • Android頁面中引導(dǎo)蒙層的使用方法詳解

    Android頁面中引導(dǎo)蒙層的使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android頁面中的引導(dǎo)蒙層使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-03-03
  • 利用Android畫圓弧canvas.drawArc()實(shí)例詳解

    利用Android畫圓弧canvas.drawArc()實(shí)例詳解

    這篇文章主要給大家介紹了關(guān)于利用Android畫圓弧canvas.drawArc()的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的理解和學(xué)習(xí)具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Android設(shè)計(jì)模式之單例模式實(shí)例

    Android設(shè)計(jì)模式之單例模式實(shí)例

    這篇文章主要介紹了Android設(shè)計(jì)模式之單例模式實(shí)例,單例模式是運(yùn)用最廣泛的設(shè)計(jì)模式之一,在應(yīng)用這個(gè)模式時(shí),單例模式的類必須保證只有一個(gè)實(shí)例存在
    2023-04-04

最新評(píng)論