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

تفصيل طريقة سريعة وسهلة لتحقيق نمط الليل في Android

ChangeMode

عنوان المشروع:ChangeMode

تنفيذ نمط الليل لـ Android.

تحقيق نمط الليل بطرق بسيطة، يدعم ListView وRecyclerView.

عرض

استخدام 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);
//إضافة عرض إضافي إلى إدارة الليل
// 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. في نداء 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="夜间模式切换 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三个属性。如需当前页面立即刷新,需填加相应属性。

属性 描述

textColorAttr 修改字体颜色时设置。如 R.attr.zztextColor 传 zztextColor 即可。例:app:textColorAttr="zztextColor"
backgroundAttr 修改背景颜色/背景图片时设置。同上。例: app:backgroundAttr="zzbackground"
تعديل لون الخلفية أو صورة الخلفية عند تعديل backgroundDrawableAttr. كما هو الحال في السابق. مثال: app:backgroundDrawableAttr="zzbackground"

الخطوة الرابعة: استدعاء الكود الـ 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));
عني
مطور أندرويد في زهونغتشو.

الرخصة
======= حقوق النشر 2016 زهانغ كي
المعتمد تحت رخصة Apache، إصدار 2.0 (الـ "الرخصة"); لا يمكنك استخدام هذا الملف إلا وفقًا للرخصة. يمكنك الحصول على نسخة من الرخصة في http://www.apache.org/licenses/LICENSE-2.0 ما لم يُطلب منك القانون المعمول به أو يتم الاتفاق عليها كتابيًا، يتم توزيع البرمجيات الموزعة تحت الرخصة على أساس "كما هو" بدون أي ضمانات أو شروط من أي نوع، سواء كانت صريحة أو ضمنية. انظر إلى الرخصة للحصول على اللغة الخاصة بموافقات وتقييدات الرخصة.

ما تم ذكره أعلاه هو شرح مفصل لطريقة بسيطة وسريعة لتحقيق نمط الليل في Android، نأمل أن يكون هذا مفيداً لكم، إذا كان لديكم أي استفسارات، فلا تترددوا في ترك تعليق، وسأقوم بالرد على تعليقاتكم في أقرب وقت ممكن، وأشكركم أيضًا على دعمكم لموقع呐喊 لتعليم البرمجة!

البيان: محتوى هذا المقال تم جمعه من الإنترنت، ويتمتع ملكية حقوق الطبع والنشر للمالك الأصلي، تم جمع المحتوى من قبل المستخدمين عبر الإنترنت الذين قدموه بأنفسهم وتم تحميله، لا يمتلك هذا الموقع حقوق الملكية، لم يتم تعديل المحتوى بشكل يدوي، ولا يتحمل هذا الموقع أي مسؤولية قانونية مرتبطة بذلك. إذا كنت قد وجدت محتوى يشتبه في انتهاك حقوق الطبع والنشر، فيرجى إرسال بريد إلكتروني إلى: notice#oldtoolbag.com (يرجى استبدال # ب @ عند إرسال البريد الإلكتروني) لإبلاغنا، وقدم الأدلة ذات الصلة، إذا تم التحقق من صحة الشكوى، سيتم حذف المحتوى المشبوه فوراً.

أنت قد تعجبك