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

تحقيق التبديل السلس بين النمط اليومي والليلي باستخدام 3 سطور من كود Android

استخدام xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //إذا كان يجب على هذه الصفحة تحديث نفسها على الفور، هنا يمكنك إدخال اسم الخاصية مثل R.attr.zzbackground ولكن يمكنك فقط إدخال zzbackground android:textColor= ?attr/zztextColor app:textColorAttr= zztextColor // 

مثال على التطبيق

 

استخدام 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) {
   // في الصفحة التي يجب أن يتم فيها التبديل الفوري، دع هذا النهج
   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();
  // في called 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>

 الخطوة الثانية:ضبط ملف نمط الليل 

<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="النمط الليلي التبديل بواسطة السيد زك" />
 <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 (عند إرسال البريد الإلكتروني، يرجى استبدال # ب @) للإبلاغ، وقدم الدليل على الدليل، وإذا تم التحقق من ذلك، فإن هذا الموقع سيزيل محتوى يشتبه في انتهاك حقوق النسخ فورًا.

أنت قد تحب