English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
ChangeMode
プロジェクト住所:ChangeMode
Androidのための夜間モードの実装。
最もシンプルな方法で夜間モードを実現し、ListView、RecyclerView をサポートします。
プレビュー
Usage xml
android:background="?attr/zzbackground" app:backgroundAttr="zzbackground"//現在のページをすぐに更新する場合、ここに属性名を渡します。例えば、R.attr.zzbackground を渡すと zzbackground になります。 android:textColor="?attr/zztextColor" app:textColorAttr="zztextColor"//ページの効果をすぐに更新する場合は、同上
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); //Add additional view to night management // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary); //ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent); // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent); //2. 切り替えの設定 //ChangeModeController.changeDay(this, R.style.DayTheme); //ChangeModeController.changeNight(this, R.style.NightTheme); } @Override protected void onDestroy() { super.onDestroy(); //3. In the onDestroy call ChangeModeController.onDestory(); }
詳細な操作説明
第1ステップ:カスタム属性の設定
<?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>
第2ステップ:夜間スタイルファイルの設定
<resources> !-- ベースアプリケーションテーマ。 --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> !-- テーマをここでカスタマイズしてください。 --> <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.DarkActionBar"> /> <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>
第3歩:レイアウトファイルで対応する属性を使用する設定を行います
<?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の3つの属性。現在のページをすぐにリフレッシュする必要がある場合は、対応する属性を追加する必要があります。
属性 説明
textColorAttrでフォント色を変更する場合の設定。例えば、R.attr.zztextColorをzztextColorに渡すと。例:app:textColorAttr="zztextColor"
backgroundAttrで背景色を変更/背景画像を設定。同上。例: app:backgroundAttr="zzbackground"
backgroundDrawableAttrで背景色を変更/背景画像を設定。同上。例: app:backgroundDrawableAttr="zzbackground"
ステップ4:ページで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);//Toggle night mode } @Override protected void onDestroy() { super.onDestroy(); //3. In the onDestroy call ChangeModeController.onDestory(); }
Three steps of code calls can start the night journey. If there are newly created views on the page that need to be added to the night mode control, the code call is as follows:
//Add additional view to night management // ChangeModeController.getInstance().addBackgroundColor(toolbar, R.attr.colorPrimary); //ChangeModeController.getInstance().addBackgroundDrawable(view,R.attr.colorAccent); // ChangeModeController.getInstance().addTextColor(view,R.attr.colorAccent);
If there are other non-standard-defined attributes when changing the night mode, you can call the following code after ChangeModeController.changeDay or ChangeModeController.changeNight to assign values to the relevant attributes:
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-20 除非适用法律或书面同意,否则在许可证下分发的软件按“原样”分發,不提供任何明示或暗示的保证或条件。有关许可证下的权限和限制的具体语言,请参阅许可证。
以上は編集者が皆さんに紹介するAndroidの夜間モードの簡単な実現方法の詳細な説明です。皆さんの助けになれば幸いです。何かご不明な点がございましたら、コメントをお願いします。編集者は皆さんの質問に迅速に回答します。また、皆さんの呐喊教程サイトへのサポートに感謝します。
声明:この記事の内容はインターネットから収集され、著作権者に帰属します。インターネットユーザーが自発的に貢献し、自己でアップロードしたものであり、本サイトは所有権を有しておらず、人工的な編集は行われていません。著作権侵害が疑われる内容がある場合は、以下のメールアドレスにご連絡ください:notice#oldtoolbag.com(メールを送信する際には、#を@に変更してください)で通報し、関連する証拠を提供してください。一旦確認がとれましたら、本サイトは即座に侵害疑いのコンテンツを削除します。