세 줄 의 Android 코드 가 낮 과 밤 모드 의 원활 한 전환 을 실현 합 니 다.
시범 효과
  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) {
   //                 
   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);
  //    view     
  // 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();
 } 
자세 한 조작 설명첫 번 째 단계:사용자 정의 속성
 <?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>
 관련 속성 에 대응 하 는 모드 의 속성 값 을 설정 합 니 다: 
<?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 세 가지 속성 을 주의 하 십시오.현재 페이지 를 즉시 새로 고침 하려 면 해당 속성 을 입력 해 야 합 니 다.
 STEP 4:페이지 호출 자바 코드
 @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();
 } 
코드 를 세 단계 호출 하면 야간 여행 을 시작 할 수 있 습 니 다.페이지 에 새로 만 든 보기 가 야간 모드 제어 에 추가 하려 면 안 드 로 이 드 소스 코드 호출:
   //    view     
  // 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
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.