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

首頁 > 系統(tǒng) > Android > 正文

Android 實(shí)現(xiàn)夜間模式的快速簡單方法實(shí)例詳解

2019-12-12 05:16:05
字體:
供稿:網(wǎng)友

ChangeMode

項(xiàng)目地址:ChangeMode

Implementation of night mode for Android.

用最簡單的方式實(shí)現(xiàn)夜間模式,支持ListView、RecyclerView。

Preview

ChangeMode

Usage xml

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

java

@Overrideprotected void onCreate(Bundle savedInstanceState) {//1. 在要立即切換效果的頁面調(diào)用此方法ChangeModeController.getInstance().init(this,R.attr.class).setTheme(this, R.style.DayTheme, R.style.NightTheme);//在其他頁面調(diào)用此方法 //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);//2. 設(shè)置切換//ChangeModeController.changeDay(this, R.style.DayTheme);//ChangeModeController.changeNight(this, R.style.NightTheme);}@Overrideprotected void onDestroy() {super.onDestroy();//3. 在 onDestroy 調(diào)用ChangeModeController.onDestory();}

詳細(xì)操作描述

第一步:自定義屬性

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

為相關(guān)屬性設(shè)置對應(yīng)模式的屬性值:

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

第三步:在布局文件中配置使用對應(yīng)屬性

<?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.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"app:theme="@style/AppTheme.AppBarOverlay"><android.support.v7.widget.Toolbarandroid: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><Buttonandroid: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.RecyclerViewandroid:id="@+id/recyclerView"android:layout_width="match_parent"android:layout_height="match_parent"android:scrollbars="vertical"/></LinearLayout>

注意textColorAttr、backgroundAttr、backgroundDrawableAttr三個(gè)屬性。如需當(dāng)前頁面立即刷新,需填加相應(yīng)屬性。

屬性 描述

textColorAttr 修改字體顏色時(shí)設(shè)置。如 R.attr.zztextColor 傳 zztextColor 即可。例:app:textColorAttr="zztextColor"
backgroundAttr 修改背景顏色/背景圖片時(shí)設(shè)置。同上。例: app:backgroundAttr="zzbackground"
backgroundDrawableAttr 修改背景顏色/背景圖片時(shí)設(shè)置。同上。例: app:backgroundDrawableAttr="zzbackground"

第四步:頁面調(diào)用 java 代碼

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

代碼調(diào)用三步,即可開始夜間之旅。 如果頁面有新創(chuàng)建的視圖要加入夜間模式控制,代碼調(diào)用:

//添加額外 view 至夜間管理// ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary);//ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent);// ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);

如果在改變夜間模式時(shí)有其他非標(biāo)準(zhǔn)定義的屬性時(shí),可在ChangeModeController.changeDay或ChangeModeController.changeNight之后調(diào)用如下代碼給相關(guān)屬性賦值:

TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
About me
An Android Developer in ZhengZhou.

License
======= Copyright 2016 zhangke
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

以上所述是小編給大家介紹的Android 實(shí)現(xiàn)夜間模式的快速簡單方法實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會(huì)及時(shí)回復(fù)大家的,在此也非常感謝大家對武林網(wǎng)網(wǎng)站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴宁市| 都兰县| 民权县| 上犹县| 柳江县| 洪雅县| 日照市| 德清县| 和田县| 应城市| 贵阳市| 庐江县| 库伦旗| 五指山市| 竹北市| 连山| 阜宁县| 稻城县| 天祝| 五华县| 靖安县| 禹州市| 武平县| 朔州市| 普定县| 顺平县| 高邮市| 弋阳县| 忻城县| 琼海市| 崇阳县| 富平县| 吴堡县| 嘉义县| 开阳县| 神池县| 嘉兴市| 峨眉山市| 寿阳县| 沁源县| 海阳市|