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

세 줄 안드로이드 코드로 일상과 밤낮 모드 원활 전환

Usage xml android:background= ?attr/zzbackground app:backgroundAttr= zzbackground //현재 페이지가 즉시 갱신되어야 한다면 여기에 속성 이름을 입력하세요. 예를 들어 R.attr.zzbackground를 전달하면 zzbackground만 전달할 수 있습니다 android:textColor= ?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();
 } 

상세 작업 설명

第一步:사용자 정의 속성

 <?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>
 <!-- 기본 애플리케이션 테마. -->
 <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.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="&#"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 세 가지 속성에 유의하세요. 현재 페이지를 즉시 새로 고침하려면 해당 속성을 추가하세요.

제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();
 } 

코드 호출 세 단계로 밤 여행을 시작할 수 있습니다.
페이지에 새로 생성된 뷰를 밤간당 모드 제어에 추가하려면, 안드로이드 소스 코드 호출:

   //추가적인 뷰를 밤간당 관리에 추가
  // 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://다운로드.jb51.net/201609/원마/AndroidChangeMode(jb51.net).rar

이것이 이 문서의 모든 내용입니다. 여러분의 학습에 도움이 되길 바라며, 다른 사람들도 지적教程을 많이 지지해 주시길 바랍니다.

선언: 이 문서의 내용은 인터넷에서 가져왔으며, 저작권자가 소유하고 있으며, 인터넷 사용자가 자발적으로 기여하고 업로드한 내용입니다. 이 사이트는 소유권을 가지고 있지 않으며, 인공적으로 편집되지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 위반이 의심되는 내용이 있으시면, 이메일을 보내 주시기 바랍니다: notice#oldtoolbag.com(이메일 보내기 때, #을 @으로 변경하십시오. 신고하고 관련 증거를 제공하시면, 사이트는 즉시 위반 내용을 삭제합니다.

추천 항목