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

首頁 > 系統 > Android > 正文

三行Android代碼實現白天夜間模式流暢切換

2019-12-12 05:16:39
字體:
來源:轉載
供稿:網友

Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //如果當前頁面要立即刷新,這里傳入屬性名稱 比如R.attr.zzbackground 傳zzbackground即可 android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor // 

演示效果

 

Usage xml    

android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"http://如果當前頁面要立即刷新,這里傳入屬性名稱 比如R.attr.zzbackground 傳zzbackground即可  android:textColor="?attr/zztextColor" app:textColorAttr="zztextColor"http://如需立即刷新頁面效果 同上 

java

 @Override protected void onCreate(Bundle savedInstanceState) {   // 在要立即切換效果的頁面調用此方法   ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);   //在其他頁面調用此方法    //ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  //添加額外view至夜間管理  // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);  //ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);  // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);  // 設置切換  //ChangeModeController.changeDay(this, R.style.DayTheme);  //ChangeModeController.changeNight(this, R.style.NightTheme); }    @Override protected void onDestroy() {  super.onDestroy();  // 在onDestroy調用  ChangeModeController.onDestory(); } 

詳細操作描述

第一步:自定義屬性

 <?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>

 第二步:配置夜間style文件 

<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">  <!-- Customize your theme here. -->  <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> <!--日間模式 --> <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> <!--夜間模式 --> <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>

 為相關屬性設置對應模式的屬性值: 

<?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> 

第三步:在布局文件中配置使用對應屬性

 <?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="夜間模式切換by 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> 

注意textColorAttr、backgroundAttr、backgroundDrawableAttr三個屬性。如需當前頁面立即刷新,需填加相應屬性。

第四步:頁面調用java代碼

 @Override protected void onCreate(Bundle savedInstanceState) {   //1. 在要立即切換效果的頁面調用此方法   ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);   //在其他頁面調用此方法    //ChangeModeController.setTheme(this, R.style.DayTheme, R.style.NightTheme);  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  //2.設置切換夜間活日間模式  //ChangeModeController.changeDay(this, R.style.DayTheme);//切換日間模式  //ChangeModeController.changeNight(this, R.style.NightTheme);//切換夜間模式 } @Override protected void onDestroy() {  super.onDestroy();  //3. 在onDestroy調用  ChangeModeController.onDestory(); } 

代碼調用三步,即可開始夜間之旅。
如果頁面有新創建的視圖要加入夜間模式控制,安卓源碼調用:

   //添加額外view至夜間管理  // 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.VeVB.COm/201609/yuanma/AndroidChangeMode(VeVB.COm).rar

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 旌德县| 阜平县| 上犹县| 平江县| 曲阳县| 长白| 南平市| 崇阳县| 新余市| 共和县| 彭山县| 永安市| 丁青县| 牙克石市| 清苑县| 济阳县| 秦安县| 临西县| 长海县| 麟游县| 乌拉特中旗| 曲阜市| 黄龙县| 府谷县| 轮台县| 肇源县| 响水县| 南丰县| 太仆寺旗| 喀喇沁旗| 玉田县| 沽源县| 达孜县| 麻江县| 苍山县| 都江堰市| 甘南县| 顺平县| 大同县| 绥芬河市| 昌乐县|