English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //現在のページをすぐにリフレッシュする場合、ここに属性名を入力します。例えばR.attr.zzbackground、zzbackgroundを渡します android:textColor= & #63;attr/zztextColor app:textColorAttr= zztextColor //
デモ効果
Usage xml
android:background=""63;attr/zzbackground" app:backgroundAttr="zzbackground"//現在のページをすぐにリフレッシュする場合、ここに属性名を入力します。例えばR.attr.zzbackground、zzbackgroundを渡します android:textColor=""63;attr/zztextColor" app:textColorAttr="zztextColor"//ページ効果をすぐにリフレッシュする必要がある場合、同上
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); //追加エクストラビューを夜間管理に // 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(); }
詳細な操作説明
ステップ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=""63;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=""63;attr/actionBarSize" android:background=""63;attr/colorPrimary" app:backgroundAttr="colorPrimary" app:titleTextColor=""63;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=""63;attr/zztextColor" app:textColorAttr="zztextColor" android:background=""63;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(); }
コードの呼び出し手順を三つにして、夜間の旅を始めることができます。
ページに新規作成されたビューを夜間モード制御に追加する場合、アンドロイドソースコードでの呼び出し:
//追加エクストラビューを夜間管理に // 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(メールを送信する際、#を@に置き換えてください。報告を行い、関連する証拠を提供してください。一旦確認がとれましたら、本サイトは即座に侵害を疑われるコンテンツを削除します。)