English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Realizzazione fluida del passaggio tra modalità giorno e notte con tre righe di codice Android

Utilizzo xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground // Se la pagina corrente deve essere aggiornata immediatamente, qui passare il nome dell'attributo, ad esempio R.attr.zzbackground, passare zzbackground android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor // 

Esempio di effetto

 

Utilizzo xml    

android:background="?attr/zzbackground"
 app:backgroundAttr="zzbackground"// Se la pagina corrente deve essere aggiornata immediatamente, qui passare il nome dell'attributo, ad esempio R.attr.zzbackground, passare zzbackground 
 android:textColor="?attr/zztextColor"
 app:textColorAttr="zztextColor"// Se si desidera aggiornare immediatamente l'effetto della pagina, come sopra 

java

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   // Chiamata di questo metodo nella pagina che deve avere un effetto di commutazione immediato
   ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
   // Chiamata di questo metodo su altre pagine 
   // ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // aggiungi view aggiuntivo alla gestione notturna
  // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
  //ChangeModeController.getInstance().addBackgroundDrawable(view, R.attr.colorAccent);
  // ChangeModeController.getInstance().addTextColor(view, R.attr.colorAccent);
  // Impostazione di commutazione
  //ChangeModeController.changeDay(this, R.style.DayTheme);
  //ChangeModeController.changeNight(this, R.style.NightTheme);
 }
 @Override
 protected void onDestroy() {
  super.onDestroy();
  // Chiamato in onDestroy
  ChangeModeController.onDestory();
 } 

Descrizione dettagliata delle operazioni

Passo 1:Proprietà personalizzate

 <?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="zzbackground" format="color|reference"/>
    <attr name="zzbackgroundDrawable" format="reference"/>
    <attr name="zztextColor" format="color"/>
    <attr name="zzItemBackground" format="color"/>
</resources>

 Passo 2:Configurazione file di stile notturno 

<resources>
 <!-- Base application theme. -->
 <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
  <!-- Personalizza il tuo tema qui. -->
  <item name="colorPrimary">@color/colorPrimary</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
  <item name="colorAccent">@color/colorAccent</item>
  <item name="windowActionBar">false</item>
  <item name="android:windowNoTitle">true</item>
  <item name="windowNoTitle">true</item>
 </style>
 <!--Modalità diurna -->
 <style name="DayTheme" parent="AppTheme">
  <item name="zzbackground">@color/dayBackground</item>
  <item name="zzbackgroundDrawable">@drawable/ic_launcher</item>
  <item name="zztextColor">@color/dayTextColor</item>
  <item name="zzItemBackground">@color/dayItemBackground</item>
 </style>
 <!--Modalità notturna -->
 <style name="NightTheme" parent="AppTheme">
  <item name="zzbackground">@color/nightBackground</item>
  <item name="zzbackgroundDrawable">@color/nightBackground</item>
  <item name="zztextColor">@color/nightTextColor</item>
  <item name="zzItemBackground">@color/nightItemBackground</item>
  <item name="colorPrimary">@color/colorPrimaryNight</item>
  <item name="colorPrimaryDark">@color/colorPrimaryDarkNight</item>
  <item name="colorAccent">@color/colorAccentNight</item>
 </style>
 <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
 <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>

 Impostare i valori delle proprietà corrispondenti per il modello: 

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="dayBackground">#F2F4F7</color>
 <color name="dayTextColor">#000</color>
 <color name="dayItemBackground">#fff</color>
 <color name="nightItemBackground">#37474F</color>
 <color name="nightBackground">#263238</color>
 <color name="nightTextColor">#fff</color>
</resources> 

Passo 3:Configurare l'uso delle proprietà corrispondenti nel file di layout

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:orientation="vertical"
 android:background="?attr/zzbackground"
 app:backgroundAttr="zzbackground"
 tools:context="com.thinkfreely.changemode.MainActivity">
 <android.support.design.widget.AppBarLayout
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:theme="@style/AppTheme.AppBarOverlay">
  <android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"
   android:background="?attr/colorPrimary"
   app:backgroundAttr="colorPrimary"
   app:titleTextColor="?attr/zztextColor"
   app:popupTheme="@style/AppTheme.PopupOverlay"
   )/>
 </android.support.design.widget.AppBarLayout>
  <Button
   android:layout_width="match_parent"
   android:layout_height="120dp"
   android:gravity="center"
   android:textColor="?attr/zztextColor"
   app:textColorAttr="zztextColor"
   android:background="?attr/zzItemBackground"
   app:backgroundAttr="zzItemBackground"
   android:padding="10dp"
   android:layout_marginBottom="8dp"
   android:textSize="22sp"
   android:textAllCaps="false"
   android:text="modalità di notte attivata da Mr.Zk" />
 <android.support.v7.widget.RecyclerView
  android:id="@+id/recyclerView"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="vertical"/>
</LinearLayout> 

Attenzione agli attributi textColorAttr, backgroundAttr, backgroundDrawableAttr. Se si desidera che la pagina si aggiorni immediatamente, aggiungere gli attributi corrispondenti.

Passo quattro:Chiamata del codice java della pagina

 @Override
 protected void onCreate(Bundle savedInstanceState) {
   // 1. Chiamata di questo metodo nella pagina che deve avere un effetto di commutazione immediato
   ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);
   // Chiamata di questo metodo su altre pagine 
   // ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  // 2. Imposta la commutazione tra il tema notturno e diurno
  // ChangeModeController.changeDay(this, R.style.DayTheme);// commuta al tema diurno
  // ChangeModeController.changeNight(this, R.style.NightTheme);// commuta al tema notturno
 }
 @Override
 protected void onDestroy() {
  super.onDestroy();
  // 3. Chiamata in onDestroy
  ChangeModeController.onDestory();
 } 

Tre passaggi di codice per iniziare il viaggio notturno.
Se la pagina ha una vista appena creata da aggiungere al controllo del tema notturno, chiamata del codice Android:

   // aggiungi view aggiuntivo alla gestione notturna
  // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);
  //ChangeModeController.getInstance().addBackgroundDrawable(view, R.attr.colorAccent);
  // ChangeModeController.getInstance().addTextColor(view, R.attr.colorAccent); 

如果在改变夜间模式时有其他非标准定义的属性时,可以在ChangeModeController.changeDay或ChangeModeController.changeNight之后调用以下代码为相关属性赋值:
   TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
   toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));

源码下载地址:http://xiazai.jb51.net/201609/yuanma/AndroidChangeMode(jb51.net).rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持呐喊教程。

声明:本文内容来自网络,版权属于原作者。内容由互联网用户自发贡献并自行上传,本网站不拥有所有权,未进行人工编辑处理,也不承担相关法律责任。如果您发现涉嫌版权的内容,请发送邮件至:notice#oldtoolbag.com(发送邮件时,请将#替换为@)进行举报,并提供相关证据。一经查实,本站将立即删除涉嫌侵权内容。

Ti potrebbe interessare