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

안드로이드에서 밤 모드를 빠르고 간단하게 구현하는 방법 예제 설명

ChangeMode

프로젝트 주소:ChangeMode

Android의 밤 시간 모드 구현.

가장 간단한 방식으로 밤 시간 모드를 구현하고, ListView, RecyclerView를 지원합니다.

미리 보기

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) {
//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);
//添加额外 view 至夜间管理
// 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>

두 번째 단계: 밤 시간 style 파일 설정

<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="?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를 전달하면 됩니다. 예: app:textColorAttr="zztextColor"
backgroundAttr 배경 색상 변경/배경 이미지를 설정할 때. 위와 같습니다. 예: app:backgroundAttr="zzbackground"
backgroundDrawableAttr 배경 색상 변경/배경 이미지를 설정할 때. 위와 같습니다. 예: app:backgroundDrawableAttr="zzbackground"

4단계: 페이지에서 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();
}

代码调用三步,即可开始夜间之旅。 如果页面有新创建的视图要加入夜间模式控制,代码调用:

//添加额外 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));
About me
An Android Developer in ZhengZhou.

License
======= Copyright 2016 zhangke
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/라이선스/라이선스-2.0 필요한 경우 적용 법률에 따라나 협상서 기재된 경우에만, 이 라이선스 하에서 배포된 소프트웨어는 "그대로" 기반으로 배포되며, 명시적이거나 유추적인 보증이나 조건 없습니다. 라이선스에서 라이선스의 허가와 제한에 대한 구체적인 언어를 참조하세요.

위에 설명한 것은 저가 여러분께 안내한 Android 밤 모드 구현의 빠르고 간단한 방법 예시입니다. 여러분께 도움이 되길 바랍니다. 어떤 질문이나 의문이 있으면 댓글을 남겨 주세요. 저는 즉시 답변을 드리겠습니다. 또한, 여러분의呐喊 교본 사이트에 대한 지원에 깊이 감사드립니다!

고지사항: 이 문서의 내용은 인터넷에서 가져왔으며, 저작권자는 모두입니다. 인터넷 사용자가 자발적으로 기여하고 자체적으로 업로드한 내용으로, 이 사이트는 소유권을 가지지 않으며, 인공 편집 처리를 하지 않았으며, 관련 법적 책임도 부담하지 않습니다. 저작권 침해가 의심되는 내용을 발견하면, 이메일을 notice#w로 보내 주시기 바랍니다.3codebox.com(메일을 보내면, #을 @으로 변경해 주세요. 신고를 해 주시고, 관련 증거를 제공해 주세요. 사실이 확인되면, 이 사이트는 즉시 저작권 침해 내용을 삭제할 것입니다。)

추천해드립니다