├── AndroidMultiTheme ├── app │ ├── .gitignore │ ├── src │ │ ├── main │ │ │ ├── res │ │ │ │ ├── values-v21 │ │ │ │ │ └── styles.xml │ │ │ │ ├── mipmap-hdpi │ │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-mdpi │ │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xhdpi │ │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxhdpi │ │ │ │ │ └── ic_launcher.png │ │ │ │ ├── mipmap-xxxhdpi │ │ │ │ │ └── ic_launcher.png │ │ │ │ ├── drawable │ │ │ │ │ ├── blue_progress_rotate.xml │ │ │ │ │ ├── red_progress_rotate.xml │ │ │ │ │ ├── black_progress_rotate.xml │ │ │ │ │ ├── green_progress_rotate.xml │ │ │ │ │ ├── yellow_progress_rotate.xml │ │ │ │ │ ├── red_button_bg_selector.xml │ │ │ │ │ ├── blue_button_bg_selector.xml │ │ │ │ │ ├── black_button_bg_selector.xml │ │ │ │ │ ├── green_button_bg_selector.xml │ │ │ │ │ ├── yellow_button_bg_selector.xml │ │ │ │ │ ├── blue_ic_theme.xml │ │ │ │ │ ├── red_ic_theme.xml │ │ │ │ │ ├── black_ic_theme.xml │ │ │ │ │ ├── green_ic_theme.xml │ │ │ │ │ ├── yellow_ic_theme.xml │ │ │ │ │ ├── red_progress_horizontal.xml │ │ │ │ │ └── black_progress_horizontal.xml │ │ │ │ ├── values │ │ │ │ │ ├── dimens.xml │ │ │ │ │ ├── attrs.xml │ │ │ │ │ ├── strings.xml │ │ │ │ │ ├── styles.xml │ │ │ │ │ ├── colors.xml │ │ │ │ │ └── themes.xml │ │ │ │ ├── values-w820dp │ │ │ │ │ └── dimens.xml │ │ │ │ ├── layout │ │ │ │ │ ├── frag_sample.xml │ │ │ │ │ ├── item_theme_adpater.xml │ │ │ │ │ └── activity_main.xml │ │ │ │ └── menu │ │ │ │ │ └── dark_theme_setings.xml │ │ │ ├── java │ │ │ │ └── io │ │ │ │ │ └── github │ │ │ │ │ └── leonhover │ │ │ │ │ └── theme │ │ │ │ │ └── samples │ │ │ │ │ ├── ThemeInfo.java │ │ │ │ │ ├── ThemeApplication.java │ │ │ │ │ ├── SampleFragment.java │ │ │ │ │ ├── ThemeAdapter.java │ │ │ │ │ └── MainActivity.java │ │ │ └── AndroidManifest.xml │ │ ├── test │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── leonhover │ │ │ │ └── theme │ │ │ │ └── ExampleUnitTest.java │ │ └── androidTest │ │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── leonhover │ │ │ └── theme │ │ │ └── ExampleInstrumentedTest.java │ ├── proguard-rules.pro │ └── build.gradle ├── android-multi-theme │ ├── .gitignore │ ├── src │ │ ├── main │ │ │ ├── res │ │ │ │ └── values │ │ │ │ │ ├── strings.xml │ │ │ │ │ ├── attrs.xml │ │ │ │ │ └── ids.xml │ │ │ ├── AndroidManifest.xml │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── leonhover │ │ │ │ └── theme │ │ │ │ ├── DarkMode.java │ │ │ │ ├── IThemeObserver.java │ │ │ │ ├── base │ │ │ │ ├── BaseThemeFragment.java │ │ │ │ ├── BaseThemeActivity.java │ │ │ │ └── widget │ │ │ │ │ └── CoverImageView.java │ │ │ │ ├── widget │ │ │ │ ├── IThemeWidget.java │ │ │ │ ├── SeekBarWidget.java │ │ │ │ ├── AbsListViewWidget.java │ │ │ │ ├── LinearLayoutWidget.java │ │ │ │ ├── CompoundButtonWidget.java │ │ │ │ ├── ListViewWidget.java │ │ │ │ ├── ViewWidget.java │ │ │ │ ├── ImageViewWidget.java │ │ │ │ ├── ToolBarWidget.java │ │ │ │ ├── ProgressBarWidget.java │ │ │ │ ├── TextViewWidget.java │ │ │ │ └── AbstractThemeWidget.java │ │ │ │ ├── annotation │ │ │ │ └── MultiThemeAttrs.java │ │ │ │ ├── custom │ │ │ │ └── CoverImageWidget.java │ │ │ │ ├── ThemePreferences.java │ │ │ │ ├── ThemeViewEntities.java │ │ │ │ ├── ThemeViewCreator.java │ │ │ │ ├── ActivityTheme.java │ │ │ │ ├── MultiTheme.java │ │ │ │ ├── ThemeUtils.java │ │ │ │ └── ThemeManager.java │ │ ├── test │ │ │ └── java │ │ │ │ └── io │ │ │ │ └── github │ │ │ │ └── leonhover │ │ │ │ └── theme │ │ │ │ └── ExampleUnitTest.java │ │ └── androidTest │ │ │ └── java │ │ │ └── io │ │ │ └── github │ │ │ └── leonhover │ │ │ └── theme │ │ │ └── ExampleInstrumentedTest.java │ ├── proguard-rules.pro │ └── build.gradle ├── settings.gradle ├── .gitignore ├── gradle │ └── wrapper │ │ ├── gradle-wrapper.jar │ │ └── gradle-wrapper.properties ├── gradle.properties ├── build.gradle ├── gradlew.bat └── gradlew ├── assets └── 2016-11-09 11_57_38.gif ├── .gitignore ├── License └── README.md /AndroidMultiTheme/app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /AndroidMultiTheme/settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':android-multi-theme' 2 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /assets/2016-11-09 11_57_38.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/assets/2016-11-09 11_57_38.gif -------------------------------------------------------------------------------- /AndroidMultiTheme/.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | library 3 | 4 | -------------------------------------------------------------------------------- /AndroidMultiTheme/gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LeonHover/AndroidMultiTheme/HEAD/AndroidMultiTheme/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /AndroidMultiTheme/gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Apr 30 18:05:10 CST 2020 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.6.4-all.zip 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 16dp 6 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_progress_rotate.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_button_bg_selector.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/res/values/ids.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/DarkMode.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | /** 4 | * 暗黑模式设置类型 5 | */ 6 | public enum DarkMode { 7 | /** 8 | * 未开启 9 | */ 10 | off, 11 | /** 12 | * 开启 13 | */ 14 | on, 15 | /** 16 | * 如果支持的话,跟随系统设置,不支持就相当于打开 17 | */ 18 | followSystem 19 | } 20 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/IThemeObserver.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | /** 4 | * Created by leonhover on 16-9-27. 5 | */ 6 | 7 | public interface IThemeObserver extends Comparable { 8 | 9 | int PRIORITY_ACTIVITY = 1; 10 | int PRIORITY_VIEW = 2; 11 | 12 | int getPriority(); 13 | 14 | void onThemeChanged(int whichTheme); 15 | } 16 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/ThemeInfo.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | /** 4 | * Created by wangzongliang on 2016/11/8. 5 | */ 6 | 7 | public class ThemeInfo { 8 | public int imgRes; 9 | public int titleRes; 10 | public int descriptionRes; 11 | 12 | public ThemeInfo(int imgRes, int titleRes, int descriptionRes) { 13 | this.imgRes = imgRes; 14 | this.titleRes = titleRes; 15 | this.descriptionRes = descriptionRes; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/test/java/io/github/leonhover/theme/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/test/java/io/github/leonhover/theme/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/base/BaseThemeFragment.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.base; 2 | 3 | import androidx.fragment.app.Fragment; 4 | 5 | import io.github.leonhover.theme.ThemeViewEntities; 6 | 7 | /** 8 | * Created by leonhover on 16-10-9. 9 | */ 10 | 11 | public class BaseThemeFragment extends Fragment { 12 | 13 | private ThemeViewEntities themeViewEntities = new ThemeViewEntities(); 14 | 15 | public ThemeViewEntities getThemeViewEntities() { 16 | return themeViewEntities; 17 | } 18 | 19 | @Override 20 | public void onDestroyView() { 21 | super.onDestroyView(); 22 | themeViewEntities.clear(); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/layout/frag_sample.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/ThemeApplication.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.app.Application; 4 | 5 | import io.github.leonhover.theme.DarkMode; 6 | import io.github.leonhover.theme.MultiTheme; 7 | 8 | /** 9 | * Created by wangzongliang on 2016/10/19. 10 | */ 11 | 12 | public class ThemeApplication extends Application { 13 | 14 | static Application sApp; 15 | 16 | @Override 17 | public void onCreate() { 18 | super.onCreate(); 19 | sApp = this; 20 | initTheme(); 21 | } 22 | 23 | private void initTheme() { 24 | MultiTheme.init(this); 25 | MultiTheme.enableDebug(); 26 | MultiTheme.setDefaultAppTheme(0); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/wangzongliang/Development/Tools/Android/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /home/wangzongliang/Development/Tools/Android/android-sdk-linux/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/SampleFragment.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.os.Bundle; 4 | import androidx.annotation.NonNull; 5 | import androidx.annotation.Nullable; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | import io.github.leonhover.theme.base.BaseThemeFragment; 11 | 12 | /** 13 | * Created by wangzongliang on 18-1-26. 14 | */ 15 | 16 | public class SampleFragment extends BaseThemeFragment { 17 | 18 | @Nullable 19 | @Override 20 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 21 | return inflater.inflate(R.layout.frag_sample, container, false); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/blue_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/red_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/black_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/green_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/drawable/yellow_ic_theme.xml: -------------------------------------------------------------------------------- 1 | 7 | 10 | 11 | -------------------------------------------------------------------------------- /AndroidMultiTheme/gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | android.injected.testOnly=false 19 | android.useAndroidX=true 20 | android.enableJetifier=true -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/IThemeWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.StyleRes; 4 | import android.util.AttributeSet; 5 | import android.view.View; 6 | 7 | /** 8 | * Created by leonhover on 16-9-27. 9 | */ 10 | 11 | public interface IThemeWidget { 12 | 13 | /** 14 | * 组装主题的信息到View的TAG中。 15 | * 16 | * @param view View 17 | * @param attributeSet View在Inflate时的Attribute. 18 | */ 19 | void assemble(View view, AttributeSet attributeSet); 20 | 21 | /** 22 | * view应用主题Theme 23 | * 24 | * @param view View 25 | */ 26 | void applyTheme(View view); 27 | 28 | /** 29 | * View应用主题中的Style 30 | * 31 | * @param view 32 | * @param styleRes Style id 33 | */ 34 | void applyStyle(View view, @StyleRes int styleRes); 35 | 36 | } 37 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/menu/dark_theme_setings.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 9 | 13 | 17 | 21 | 22 | -------------------------------------------------------------------------------- /AndroidMultiTheme/build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | google() 6 | jcenter() 7 | mavenLocal() 8 | } 9 | dependencies { 10 | classpath 'com.android.tools.build:gradle:3.6.3' 11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.8.5' 12 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0' 13 | // NOTE: Do not place your application dependencies here; they belong 14 | // in the individual module build.gradle files 15 | } 16 | } 17 | 18 | plugins { 19 | id "com.jfrog.bintray" version "1.8.5" 20 | } 21 | 22 | allprojects { 23 | repositories { 24 | google() 25 | jcenter() 26 | mavenLocal() 27 | } 28 | } 29 | 30 | task clean(type: Delete) { 31 | delete rootProject.buildDir 32 | } 33 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/androidTest/java/io/github/leonhover/theme/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.leonhover.theme", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/androidTest/java/io/github/leonhover/theme/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("io.github.leonhover.theme.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 29 5 | defaultConfig { 6 | applicationId "io.github.leonhover.theme.samples" 7 | minSdkVersion 19 8 | targetSdkVersion 29 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', { 24 | exclude group: 'com.android.support', module: 'support-annotations' 25 | }) 26 | implementation project(':android-multi-theme') 27 | implementation 'androidx.appcompat:appcompat:1.1.0' 28 | implementation 'com.google.android.material:material:1.1.0' 29 | testImplementation 'junit:junit:4.12' 30 | } 31 | -------------------------------------------------------------------------------- /License: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 王宗亮 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/annotation/MultiThemeAttrs.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.annotation; 2 | 3 | import androidx.annotation.AttrRes; 4 | 5 | import java.lang.annotation.ElementType; 6 | import java.lang.annotation.Inherited; 7 | import java.lang.annotation.Retention; 8 | import java.lang.annotation.RetentionPolicy; 9 | import java.lang.annotation.Target; 10 | 11 | /** 12 | * MultiThemeAttrs是一个Attribute的数组,用来注解MultiTheme支持的主题控件类。 13 | *

14 | * 这些Attribute是具体View的对应Styleable中的值 15 | * 这些值不但是作为MultiTheme中用来索引的Key,也是通过其从XML解析出的Attribute中取出主题元素的值的关键。 16 | *

17 | * 例: 18 | * android:background="?attr/theme_background" 19 | *

20 | * View中都有'android.R.attr.background'的标签,添加其作为View的MultiThemeAttrs,那么MultiTheme 21 | * 就会将'background'标签下的'theme_background'对应attrRes取出,作为主题元素的值来存储。当主题改变 22 | * 的时候,通过'android.R.attr.background'找到attrRes,然后分发到对应的 23 | * {@link io.github.leonhover.theme.widget.ViewWidget}的'applyElementTheme'。 24 | *

25 | * Created by wangzongliang on 2017/2/14. 26 | */ 27 | @Retention(RetentionPolicy.RUNTIME) 28 | @Target(ElementType.TYPE) 29 | @Inherited 30 | public @interface MultiThemeAttrs { 31 | @AttrRes int[] value(); 32 | } 33 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/SeekBarWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.AbsSeekBar; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.thumb 16 | }) 17 | public class SeekBarWidget extends ProgressBarWidget { 18 | @Override 19 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 20 | super.applyElementTheme(view, themeElementKey, resId); 21 | AbsSeekBar seekBar = (AbsSeekBar) view; 22 | switch (themeElementKey) { 23 | case android.R.attr.thumb: 24 | setThumb(seekBar, resId); 25 | break; 26 | } 27 | } 28 | 29 | public static void setThumb(AbsSeekBar seekBar, @DrawableRes int drawableResId) { 30 | if (seekBar == null) { 31 | return; 32 | } 33 | 34 | seekBar.setThumb(getDrawable(seekBar, drawableResId)); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidMultiTheme 3 | Support multi theme for Android App without recreation of Activity. 4 | Change widget attribute dynamically! 5 | Red Theme 6 | The kid was cught red-handed drinking from his father’s wine collection. 7 | Blue Theme 8 | The newly-wed princess is a blue-blooded member of the royalty. 9 | Yellow Theme 10 | You can’t count on him; he has a yellow streak down his back. 11 | Green Theme 12 | After the feasibility studies were in, the CEO gave the green light to the new project. 13 | Black Theme 14 | In children’s literature, a character is often either all good or all bad; such balck-and-white situations seldom exist in real life. 15 | 16 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/AbsListViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.AbsListView; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.listSelector 16 | }) 17 | public class AbsListViewWidget extends ViewWidget { 18 | 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | AbsListView absListView = (AbsListView) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.listSelector: 25 | setSelector(absListView, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setSelector(AbsListView absListView, @DrawableRes int drawableResId) { 31 | if (absListView == null) { 32 | return; 33 | } 34 | 35 | absListView.setSelector(getDrawable(absListView, drawableResId)); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/LinearLayoutWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.LinearLayout; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | 12 | /** 13 | * Created by leonhover on 16-9-27. 14 | */ 15 | @MultiThemeAttrs({ 16 | android.R.attr.divider 17 | }) 18 | public class LinearLayoutWidget extends ViewWidget { 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | LinearLayout linearLayout = (LinearLayout) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.divider: 25 | setDividerDrawable(linearLayout, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setDividerDrawable(LinearLayout linearLayout, @DrawableRes int drawableResId) { 31 | if (linearLayout == null) { 32 | return; 33 | } 34 | 35 | linearLayout.setDividerDrawable(getDrawable(linearLayout,drawableResId)); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/CompoundButtonWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import androidx.annotation.AnyRes; 4 | import androidx.annotation.AttrRes; 5 | import androidx.annotation.DrawableRes; 6 | import android.view.View; 7 | import android.widget.CompoundButton; 8 | 9 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 10 | 11 | /** 12 | * Created by leonhover on 16-9-27. 13 | */ 14 | @MultiThemeAttrs({ 15 | android.R.attr.button 16 | }) 17 | public class CompoundButtonWidget extends TextViewWidget { 18 | 19 | @Override 20 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 21 | super.applyElementTheme(view, themeElementKey, resId); 22 | CompoundButton compoundButton = (CompoundButton) view; 23 | switch (themeElementKey) { 24 | case android.R.attr.button: 25 | setButtonDrawable(compoundButton, resId); 26 | break; 27 | } 28 | } 29 | 30 | public static void setButtonDrawable(CompoundButton compoundButton, @DrawableRes int drawableResId) { 31 | 32 | if (compoundButton == null) { 33 | return; 34 | } 35 | 36 | compoundButton.setButtonDrawable(getDrawable(compoundButton, drawableResId)); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 12 | 13 | 17 | 18 | 22 | 23 | 27 | 28 | 43 | 44 | 49 | ``` 50 | 51 | 4. 对Acitivity设定主题集合 52 | 53 | ``` 54 | activityTheme.setThemes(new int[]{R.style.AppTheme_Blue, 55 | R.style.AppTheme_Red}); 56 | ``` 57 | 5. 在XML中静态使用 58 | 59 | ``` 60 | 68 | ``` 69 | 70 | 6. 在代码中动态使用 71 | 72 | ``` 73 | attrChanged = !attrChanged; 74 | int textColorAttrId = attrChanged ? R.attr.sub_title_text_color : 75 | R.attr.title_text_color; 76 | MultiTheme.applySingleElementTheme(android.R.attr.textColor, textColorAttrId); 77 | ``` 78 | 79 | 7. 切换主题 80 | 81 | ``` 82 | MultiTheme.setAppTheme(indexOfthemes);//blue 0,red 1; 83 | ``` 84 | 85 | 8. 暗黑主题 86 | 87 | ``` 88 | MultiTheme.setDarkMode(darkMode); // DarkMode.off(关),DarkMode.on(开),DarkMode.followSystem(跟随系统主题); 89 | ``` 90 | 91 | ## 进阶 92 | 1. CoverImageView与CoverImageWidget 93 | 94 | 在主题的需求中,有一些情况一般是通过对图片添加不同主题的颜色蒙层来实现改变主题效果。CoverImageView就是这样的自定义ImageView,添加attribute——coverColor以及setCoverColor(int color)方法。支持对任意形状的图标控件添加颜色蒙层效果,而且不会忽略图片中的alpha值。 95 | 96 | 2. 自定义主题控件 97 | 98 | 开发中,我们都会遇到需要自定义View来满足需求得情况,进阶第1项中的CoverImageView以及CoverImageWidget就是很好的例子,可供参考。 99 | 在0.3.x版本以后,添加注解扩展主题控件属性方式,可参考TextViewWidget的编写。 100 | 101 | 3. AdapterView 102 | 103 | 由于AdapterView具有ItemView的缓存机制,所以不一定能在Window的decorView中找到,这个就需要在View被attach到window的时候应用下当前的主题Style。 104 | Sample中写了RecyclerView的使用场景。 105 | ListView的Adapter中,应该在getView方法中调用: 106 | ``` 107 | MultiTheme.applyTheme(holder.itemView); 108 | ``` 109 | 4. "放养的孩子" 110 | 111 | AdapterView中添加HeaderView或FooterView的情况,他们无法再AdapterView的Adapter回调方法中知道何时attach到Window上。这种就需要我们自己记录,并在主题切换的时候,主动对View的应用主题。 112 | `ThemeViewEntities`就是这种情况的辅助类,只需要将上面情况的View添加到`ThemeViewEntities`中,主题框架就会自动管理这些View的主题。 113 | 114 | ## 注意 115 | 1. 框架利用AppCompatActivity中的接口覆盖了XML解释的部分逻辑,所以这个框架只支持AppCompatActivity内使用。 116 | 2. 出现inflate的异常,一般情况是没有attrRes指向的资源在当前使用的Style中。 117 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/base/widget/CoverImageView.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.base.widget; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Canvas; 7 | import android.graphics.Color; 8 | import android.graphics.Paint; 9 | import android.graphics.PorterDuff; 10 | import androidx.appcompat.widget.AppCompatImageView; 11 | import android.util.AttributeSet; 12 | 13 | import java.lang.ref.SoftReference; 14 | 15 | import io.github.leonhover.theme.R; 16 | 17 | /** 18 | * Created by wangzongliang on 2016/11/8. 19 | */ 20 | 21 | public class CoverImageView extends AppCompatImageView { 22 | 23 | public static final String TAG = "CoverImageView"; 24 | 25 | private Paint mPaint; 26 | private SoftReference tmpBitmapReference = null; 27 | private SoftReference tmpCanvasReference = null; 28 | private int canvasWidth = -1; 29 | private int canvasHeight = -1; 30 | private int[] pixels; 31 | private int coverColor = -1; 32 | 33 | public CoverImageView(Context context) { 34 | super(context); 35 | } 36 | 37 | public CoverImageView(Context context, AttributeSet attrs) { 38 | super(context, attrs); 39 | init(context, attrs, 0); 40 | } 41 | 42 | public CoverImageView(Context context, AttributeSet attrs, int defStyleAttr) { 43 | super(context, attrs, defStyleAttr); 44 | init(context, attrs, defStyleAttr); 45 | } 46 | 47 | private void init(Context context, AttributeSet attrs, int defStyleAttr) { 48 | mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 49 | TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CoverImageView); 50 | coverColor = ta.getColor(R.styleable.CoverImageView_coverColor, -1); 51 | ta.recycle(); 52 | } 53 | 54 | public void setCoverColor(int color) { 55 | this.coverColor = color; 56 | invalidate(); 57 | } 58 | 59 | @Override 60 | protected void onDraw(Canvas canvas) { 61 | super.onDraw(canvas); 62 | 63 | if (coverColor < 0) { 64 | return; 65 | } 66 | 67 | int coverAlpha = coverColor & 0xff000000; 68 | 69 | if (coverAlpha == 0x00000000) { 70 | return; 71 | } 72 | 73 | if (getHeight() + getWidth() == 0) { 74 | return; 75 | } 76 | 77 | if (canvasHeight != getHeight() || canvasWidth != getWidth()) { 78 | canvasHeight = getHeight(); 79 | canvasWidth = getWidth(); 80 | pixels = new int[canvasWidth * canvasHeight]; 81 | } 82 | 83 | Bitmap tmpBitmap = createCoverBitmap(canvasWidth, canvasHeight); 84 | 85 | Canvas tmpCanvas = createCoverCanvas(tmpBitmap); 86 | 87 | tmpCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); 88 | super.onDraw(tmpCanvas); 89 | 90 | 91 | tmpBitmap.getPixels(pixels, 0, canvasWidth, 0, 0, canvasWidth, canvasHeight); 92 | for (int i = 0; i < pixels.length; i++) { 93 | if (pixels[i] != 0x00000000) { 94 | pixels[i] = coverColor; 95 | } 96 | } 97 | tmpBitmap.setPixels(pixels, 0, canvasWidth, 0, 0, canvasWidth, canvasHeight); 98 | canvas.drawBitmap(tmpBitmap, 0, 0, mPaint); 99 | } 100 | 101 | private Bitmap createCoverBitmap(int width, int height) { 102 | Bitmap coverBitmap = null; 103 | 104 | if (tmpBitmapReference != null && tmpBitmapReference.get() != null) { 105 | coverBitmap = tmpBitmapReference.get(); 106 | if (coverBitmap.getWidth() != width || coverBitmap.getHeight() != height || coverBitmap.isRecycled()) { 107 | coverBitmap = null; 108 | } 109 | } 110 | 111 | if (coverBitmap == null) { 112 | coverBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 113 | tmpBitmapReference = new SoftReference(coverBitmap); 114 | } 115 | 116 | return coverBitmap; 117 | } 118 | 119 | private Canvas createCoverCanvas(Bitmap bitmap) { 120 | Canvas coverCanvas = null; 121 | 122 | if (tmpCanvasReference != null && tmpCanvasReference.get() != null) { 123 | coverCanvas = tmpCanvasReference.get(); 124 | } 125 | 126 | if (coverCanvas == null) { 127 | coverCanvas = new Canvas(bitmap); 128 | tmpCanvasReference = new SoftReference(coverCanvas); 129 | } 130 | 131 | return coverCanvas; 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/TextViewWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.graphics.drawable.Drawable; 4 | import androidx.annotation.AnyRes; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.ColorRes; 7 | import androidx.annotation.DrawableRes; 8 | import android.util.Log; 9 | import android.view.View; 10 | import android.widget.TextView; 11 | 12 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 13 | 14 | /** 15 | * Created by leonhover on 16-9-27. 16 | */ 17 | @MultiThemeAttrs({ 18 | android.R.attr.textColor, 19 | android.R.attr.textColorHint, 20 | android.R.attr.textColorLink, 21 | android.R.attr.drawableBottom, 22 | android.R.attr.drawableTop, 23 | android.R.attr.drawableLeft, 24 | android.R.attr.drawableRight, 25 | }) 26 | public class TextViewWidget extends ViewWidget { 27 | 28 | @Override 29 | public void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 30 | super.applyElementTheme(view, themeElementKey, resId); 31 | TextView textView = (TextView) view; 32 | switch (themeElementKey) { 33 | case android.R.attr.textColor: 34 | setTextColor(textView, resId); 35 | break; 36 | case android.R.attr.textColorHint: 37 | setTextColorHint(textView, resId); 38 | break; 39 | case android.R.attr.textColorLink: 40 | setTextColorLink(textView, resId); 41 | break; 42 | case android.R.attr.drawableBottom: 43 | setDrawableBottom(textView, resId); 44 | break; 45 | case android.R.attr.drawableTop: 46 | setDrawableTop(textView, resId); 47 | break; 48 | case android.R.attr.drawableLeft: 49 | setDrawableLeft(textView, resId); 50 | break; 51 | case android.R.attr.drawableRight: 52 | setDrawableRight(textView, resId); 53 | break; 54 | default: 55 | } 56 | } 57 | 58 | public static void setTextColor(TextView textView, @ColorRes int colorResId) { 59 | if (textView == null) { 60 | return; 61 | } 62 | Log.d(TAG, "setTextColor textColor:"+colorResId); 63 | textView.setTextColor(getColorStateList(textView, colorResId)); 64 | } 65 | 66 | public static void setTextColorLink(TextView textView, @ColorRes int colorResId) { 67 | if (textView == null) { 68 | return; 69 | } 70 | 71 | textView.setLinkTextColor(getColorStateList(textView, colorResId)); 72 | } 73 | 74 | public static void setTextColorHint(TextView textView, @ColorRes int colorResId) { 75 | if (textView == null) { 76 | return; 77 | } 78 | 79 | textView.setHintTextColor(getColorStateList(textView, colorResId)); 80 | } 81 | 82 | public static void setDrawableBottom(TextView textView, @DrawableRes int drawableResId) { 83 | if (textView == null) { 84 | return; 85 | } 86 | 87 | Drawable drawable = getDrawable(textView, drawableResId); 88 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 89 | textView.setCompoundDrawables(null, null, null, drawable); 90 | } 91 | 92 | public static void setDrawableLeft(TextView textView, @DrawableRes int drawableResId) { 93 | if (textView == null) { 94 | return; 95 | } 96 | 97 | Drawable drawable = getDrawable(textView, drawableResId); 98 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 99 | textView.setCompoundDrawables(drawable, null, null, null); 100 | } 101 | 102 | public static void setDrawableRight(TextView textView, @DrawableRes int drawableResId) { 103 | if (textView == null) { 104 | return; 105 | } 106 | 107 | Drawable drawable = getDrawable(textView, drawableResId); 108 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 109 | textView.setCompoundDrawables(null, null, drawable, null); 110 | } 111 | 112 | public static void setDrawableTop(TextView textView, @DrawableRes int drawableResId) { 113 | if (textView == null) { 114 | return; 115 | } 116 | 117 | Drawable drawable = getDrawable(textView, drawableResId); 118 | drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); 119 | textView.setCompoundDrawables(null, drawable, null, null); 120 | } 121 | } 122 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 19 | 20 | 35 | 36 | 50 | 51 | 65 | 66 | 80 | -------------------------------------------------------------------------------- /AndroidMultiTheme/gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /AndroidMultiTheme/app/src/main/java/io/github/leonhover/theme/samples/MainActivity.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.samples; 2 | 3 | import android.content.res.Configuration; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | import androidx.appcompat.app.AppCompatDelegate; 7 | import androidx.recyclerview.widget.LinearLayoutManager; 8 | import androidx.recyclerview.widget.RecyclerView; 9 | import androidx.appcompat.widget.Toolbar; 10 | import android.util.Log; 11 | import android.view.Menu; 12 | import android.view.MenuItem; 13 | import android.view.View; 14 | import android.widget.ImageView; 15 | import android.widget.LinearLayout; 16 | import android.widget.TextView; 17 | 18 | import java.util.ArrayList; 19 | import java.util.List; 20 | 21 | import io.github.leonhover.theme.ActivityTheme; 22 | import io.github.leonhover.theme.DarkMode; 23 | import io.github.leonhover.theme.MultiTheme; 24 | import io.github.leonhover.theme.ThemeUtils; 25 | import io.github.leonhover.theme.base.BaseThemeActivity; 26 | 27 | public class MainActivity extends BaseThemeActivity { 28 | 29 | public static final String TAG = MainActivity.class.getSimpleName(); 30 | 31 | private TextView appDescriptionTextView; 32 | private boolean attrChanged = false; 33 | 34 | private RecyclerView recyclerView; 35 | ImageView mBackground; 36 | 37 | static { 38 | AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); 39 | } 40 | 41 | @Override 42 | protected void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | setContentView(R.layout.activity_main); 45 | Log.d(TAG, "isNightMode:" + ThemeUtils.isSystemDarkMode(this)); 46 | initActionBar(); 47 | 48 | appDescriptionTextView = (TextView) findViewById(R.id.app_description_text); 49 | recyclerView = (RecyclerView) findViewById(R.id.recycler_view); 50 | recyclerView.setLayoutManager(new LinearLayoutManager(this)); 51 | 52 | LinearLayout linearLayout = findViewById(R.id.contaier); 53 | 54 | mBackground = new ImageView(this); 55 | MultiTheme.applySingleElementTheme(mBackground, android.R.attr.background, R.attr.sub_title_text_color); 56 | MultiTheme.applySingleElementTheme(mBackground, android.R.attr.src, R.attr.icon_launcher); 57 | linearLayout.addView(mBackground); 58 | 59 | List themeInfoList = new ArrayList<>(); 60 | themeInfoList.add(new ThemeInfo(R.drawable.blue_ic_theme, 61 | R.string.theme_blue_title, 62 | R.string.theme_blue_description 63 | )); 64 | themeInfoList.add(new ThemeInfo(R.drawable.red_ic_theme, 65 | R.string.theme_red_title, 66 | R.string.theme_red_description 67 | )); 68 | themeInfoList.add(new ThemeInfo(R.drawable.green_ic_theme, 69 | R.string.theme_green_title, 70 | R.string.theme_green_description 71 | )); 72 | themeInfoList.add(new ThemeInfo(R.drawable.yellow_ic_theme, 73 | R.string.theme_yellow_title, 74 | R.string.theme_yellow_description 75 | )); 76 | themeInfoList.add(new ThemeInfo(R.drawable.black_ic_theme, 77 | R.string.theme_Black_title, 78 | R.string.theme_Black_description 79 | )); 80 | 81 | 82 | ThemeAdapter themeAdapter = new ThemeAdapter(this); 83 | themeAdapter.setThemeInfoList(themeInfoList); 84 | recyclerView.setAdapter(themeAdapter); 85 | 86 | } 87 | 88 | private void initActionBar() { 89 | Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 90 | this.setSupportActionBar(toolbar); 91 | } 92 | 93 | @Override 94 | public boolean onCreateOptionsMenu(Menu menu) { 95 | getMenuInflater().inflate(R.menu.dark_theme_setings, menu); 96 | return super.onCreateOptionsMenu(menu); 97 | } 98 | 99 | @Override 100 | public boolean onPrepareOptionsMenu(Menu menu) { 101 | DarkMode mode = MultiTheme.getDarkMode(); 102 | switch (mode) { 103 | case off: 104 | menu.findItem(R.id.amt_dark_theme_on).setChecked(false); 105 | menu.findItem(R.id.amt_dark_theme_off).setChecked(true); 106 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(false); 107 | return true; 108 | case on: 109 | menu.findItem(R.id.amt_dark_theme_on).setChecked(true); 110 | menu.findItem(R.id.amt_dark_theme_off).setChecked(false); 111 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(false); 112 | return true; 113 | case followSystem: 114 | menu.findItem(R.id.amt_dark_theme_on).setChecked(false); 115 | menu.findItem(R.id.amt_dark_theme_off).setChecked(false); 116 | menu.findItem(R.id.amt_dark_theme_follow_system).setChecked(true); 117 | return true; 118 | } 119 | return super.onPrepareOptionsMenu(menu); 120 | } 121 | 122 | @Override 123 | public boolean onOptionsItemSelected(@NonNull MenuItem item) { 124 | switch (item.getItemId()) { 125 | case R.id.amt_dark_theme_off: 126 | MultiTheme.setDarkMode(DarkMode.off); 127 | item.setChecked(true); 128 | return true; 129 | case R.id.amt_dark_theme_on: 130 | MultiTheme.setDarkMode(DarkMode.on); 131 | item.setChecked(true); 132 | return true; 133 | case R.id.amt_dark_theme_follow_system: 134 | MultiTheme.setDarkMode(DarkMode.followSystem); 135 | item.setChecked(true); 136 | return true; 137 | } 138 | return super.onOptionsItemSelected(item); 139 | } 140 | 141 | @Override 142 | protected void configTheme(ActivityTheme activityTheme) { 143 | activityTheme.setThemes(4, new int[]{R.style.AppTheme_Blue, R.style.AppTheme_Red, R.style.AppTheme_Green, R.style.AppTheme_Yellow, R.style.AppTheme_Black}); 144 | activityTheme.setStatusBarColorAttrRes(R.attr.colorPrimary); 145 | activityTheme.setSupportMenuItemThemeEnable(true); 146 | } 147 | 148 | public void clickButton(View view) { 149 | attrChanged = !attrChanged; 150 | int textColorAttrId = attrChanged ? R.attr.sub_title_text_color : R.attr.title_text_color; 151 | MultiTheme.applySingleElementTheme(appDescriptionTextView, android.R.attr.textColor, textColorAttrId); 152 | } 153 | 154 | @Override 155 | public void onConfigurationChanged(Configuration newConfig) { 156 | super.onConfigurationChanged(newConfig); 157 | Log.d(TAG, "onConfigurationChanged uiMode:" + newConfig.uiMode); 158 | switch (newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK) { 159 | case Configuration.UI_MODE_NIGHT_YES: 160 | Log.d(TAG, "UI_MODE_NIGHT_YES"); 161 | break; 162 | case Configuration.UI_MODE_NIGHT_NO: 163 | Log.d(TAG, "UI_MODE_NIGHT_NO"); 164 | break; 165 | case Configuration.UI_MODE_NIGHT_UNDEFINED: 166 | Log.d(TAG, "UI_MODE_NIGHT_UNDEFINED"); 167 | break; 168 | } 169 | } 170 | } 171 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ActivityTheme.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.annotation.TargetApi; 4 | import android.os.Build; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.ColorInt; 7 | import androidx.annotation.StyleRes; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | 10 | import android.view.View; 11 | import android.view.ViewGroup; 12 | import android.view.Window; 13 | import android.view.WindowManager; 14 | 15 | import static io.github.leonhover.theme.ThemeUtils.IS_KITKAT; 16 | import static io.github.leonhover.theme.ThemeUtils.IS_LOLLIPOP; 17 | import static io.github.leonhover.theme.ThemeUtils.getStatusBarHeight; 18 | 19 | /** 20 | * Created by wangzongliang on 2016/10/28. 21 | */ 22 | 23 | public class ActivityTheme implements IThemeObserver { 24 | 25 | private AppCompatActivity activity; 26 | 27 | @StyleRes 28 | private int[] themes; 29 | 30 | private int darkThemeIndex = -1; 31 | 32 | private boolean isSupportMenuItemEnable = false; 33 | 34 | @AttrRes 35 | private int statusBarColorAttrResId = 0; 36 | 37 | private View statusBarPlaceHolder = null; 38 | 39 | public ActivityTheme(AppCompatActivity activity) { 40 | this.activity = activity; 41 | } 42 | 43 | /** 44 | * 设置主题资源数组 45 | * 46 | * @param themes 主题资源数组 47 | */ 48 | public final void setThemes(@StyleRes int[] themes) { 49 | this.themes = themes; 50 | } 51 | 52 | /** 53 | * 设置支持暗黑模式的主题资源数组 54 | * 55 | * @param darkThemeIndex 暗黑主题在主题资源数组中的位置 56 | * @param themes 主题资源数组 57 | */ 58 | public final void setThemes(int darkThemeIndex, @StyleRes int[] themes) { 59 | this.themes = themes; 60 | this.darkThemeIndex = darkThemeIndex; 61 | if (this.darkThemeIndex < 0 || this.darkThemeIndex >= this.themes.length) { 62 | throw new IllegalArgumentException("please check you param,there ara some error."); 63 | } 64 | } 65 | 66 | /** 67 | * 开启主题切换支持菜单功能 68 | * 69 | * @param enable 70 | */ 71 | public final void setSupportMenuItemThemeEnable(boolean enable) { 72 | this.isSupportMenuItemEnable = enable; 73 | } 74 | 75 | /** 76 | * 设置通知栏的颜色,4.4以下的设备无效 77 | * 78 | * @param statusBarColorAttrResId 指向颜色值的AttrRes 79 | */ 80 | public final void setStatusBarColorAttrRes(@AttrRes int statusBarColorAttrResId) { 81 | this.statusBarColorAttrResId = statusBarColorAttrResId; 82 | } 83 | 84 | public final void setStatusBarColor(@ColorInt int statusBarColor) { 85 | if (!IS_KITKAT) { 86 | return; 87 | } 88 | 89 | if (IS_LOLLIPOP) { 90 | setStatusBarColorOnLollipop(statusBarColor); 91 | } else if (IS_KITKAT) { 92 | setStatusBarColorKitKat(statusBarColor); 93 | } 94 | } 95 | 96 | public void assembleThemeBeforeInflate() { 97 | MultiTheme.addObserver(this); 98 | int whichTheme = MultiTheme.getAppTheme(); 99 | int theme = getTheme(whichTheme); 100 | if (theme > 0) { 101 | activity.setTheme(theme); 102 | } 103 | MultiTheme.assembleThemeBeforeInflate(activity); 104 | initializeStatusBarTheme(); 105 | } 106 | 107 | private void initializeStatusBarTheme() { 108 | MultiTheme.d(MultiTheme.TAG, "initializeStatusBarTheme sdk version:" + Build.VERSION.SDK_INT); 109 | if (!IS_KITKAT || statusBarColorAttrResId == 0) { 110 | return; 111 | } 112 | 113 | int statusColor = ThemeUtils.getColor(this.activity, statusBarColorAttrResId); 114 | if (IS_LOLLIPOP) { 115 | initializeStatusBarColorOnLollipop(statusColor); 116 | } else { 117 | initializeStatusBarColorKitKat(statusColor); 118 | } 119 | } 120 | 121 | @TargetApi(Build.VERSION_CODES.KITKAT) 122 | private void initializeStatusBarColorKitKat(int statusBarColor) { 123 | MultiTheme.d(MultiTheme.TAG, "setStatusBarColorKitkat"); 124 | int statusBarHeight = getStatusBarHeight(this.activity); 125 | Window window = activity.getWindow(); 126 | ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT); 127 | 128 | //First translucent status bar. 129 | window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 130 | 131 | ViewGroup actionBarRoot = (ViewGroup) mContentView.getParent(); 132 | 133 | statusBarPlaceHolder = new View(activity); 134 | ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, statusBarHeight); 135 | //向 ContentView 中添加假 View 136 | actionBarRoot.addView(statusBarPlaceHolder, 0, lp); 137 | setStatusBarColorKitKat(statusBarColor); 138 | } 139 | 140 | private void setStatusBarColorKitKat(int statusBarColor) { 141 | if (statusBarPlaceHolder != null) { 142 | statusBarPlaceHolder.setBackgroundColor(statusBarColor); 143 | } 144 | } 145 | 146 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 147 | private void initializeStatusBarColorOnLollipop(int statusBarColor) { 148 | MultiTheme.d(MultiTheme.TAG, "setStatusBarColorOnLollipop"); 149 | Window window = activity.getWindow(); 150 | //设置透明状态栏,这样才能让 ContentView 向上 151 | window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); 152 | 153 | //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色 154 | window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); 155 | //设置状态栏颜色 156 | window.setStatusBarColor(statusBarColor); 157 | } 158 | 159 | @TargetApi(Build.VERSION_CODES.LOLLIPOP) 160 | private void setStatusBarColorOnLollipop(int statusBarColor) { 161 | 162 | Window window = activity.getWindow(); 163 | window.setStatusBarColor(statusBarColor); 164 | } 165 | 166 | public void destroy() { 167 | MultiTheme.removeObserver(this); 168 | this.activity = null; 169 | } 170 | 171 | @Override 172 | public int getPriority() { 173 | return PRIORITY_ACTIVITY; 174 | } 175 | 176 | @Override 177 | public final void onThemeChanged(int whichTheme) { 178 | MultiTheme.applyTheme(this.activity, getTheme(whichTheme)); 179 | changeStatusBarColor(); 180 | if (isSupportMenuItemEnable) { 181 | this.activity.supportInvalidateOptionsMenu(); 182 | } 183 | } 184 | 185 | private void changeStatusBarColor() { 186 | if (statusBarColorAttrResId != 0) { 187 | int statusBarColor = ThemeUtils.getColor(this.activity, statusBarColorAttrResId); 188 | setStatusBarColor(statusBarColor); 189 | } 190 | } 191 | 192 | private int getTheme(int index) { 193 | if (this.themes == null) { 194 | MultiTheme.e(MultiTheme.TAG, "There is no theme array."); 195 | return -1; 196 | } 197 | 198 | if (index == MultiTheme.DARK_THEME) { 199 | return getDarkTheme(); 200 | } else { 201 | return getNormalTheme(index); 202 | } 203 | } 204 | 205 | private int getNormalTheme(int index) { 206 | MultiTheme.d(MultiTheme.TAG, "getNormalTheme index:" + index); 207 | if (index > this.themes.length || index < 0) { 208 | MultiTheme.e(MultiTheme.TAG, "OutOfBound. we use the first theme."); 209 | return this.themes[0]; 210 | } 211 | return this.themes[index]; 212 | } 213 | 214 | private int getDarkTheme() { 215 | MultiTheme.d(MultiTheme.TAG, "getDarkTheme"); 216 | if (darkThemeIndex < 0 || darkThemeIndex >= this.themes.length) { 217 | MultiTheme.e(MultiTheme.TAG, "You forgot setup a darkThemeIndex, we use the first theme indeed."); 218 | return this.themes[0]; 219 | } 220 | return this.themes[this.darkThemeIndex]; 221 | } 222 | 223 | @Override 224 | public int compareTo(IThemeObserver o) { 225 | return getPriority() > o.getPriority() ? 1 : -1; 226 | } 227 | } 228 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/MultiTheme.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import androidx.annotation.AttrRes; 6 | import androidx.annotation.StyleRes; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import android.util.Log; 9 | import android.view.View; 10 | 11 | import io.github.leonhover.theme.widget.AbstractThemeWidget; 12 | 13 | /** 14 | * Created by wangzongliang on 2016/10/28. 15 | */ 16 | 17 | public class MultiTheme { 18 | 19 | static final String TAG = MultiTheme.class.getSimpleName(); 20 | public static final int DARK_THEME = 2016; 21 | public static int sDefaultThemeIndex = 0; 22 | private static ThemeManager sThemeManager = null; 23 | private static boolean debugMode = false; 24 | 25 | /** 26 | * 初始化多主题模块,必须在其他方法调用前调用 27 | */ 28 | public static void init(Application application) { 29 | sThemeManager = new ThemeManager(application); 30 | } 31 | 32 | /** 33 | * 设置调试模式 34 | * 35 | * @param debugMode 36 | * @deprecated 37 | */ 38 | public static void setDebugMode(boolean debugMode) { 39 | MultiTheme.debugMode = debugMode; 40 | } 41 | 42 | public static void enableDebug() { 43 | debugMode = true; 44 | } 45 | 46 | /** 47 | * 添加自定义的AbstractThemeWidget,通常紧随init方法后调用。 48 | * 49 | * @param widgetKey 类型索引,指定themeWidget对应的View的Class,如TextViewWidget对应TextView.class. 50 | * @param themeWidget 自定义的AbstractThemeWidget 51 | */ 52 | public static void addThemeWidget(Class widgetKey, AbstractThemeWidget themeWidget) { 53 | checkInstance(); 54 | sThemeManager.addThemeWidget(widgetKey, themeWidget); 55 | } 56 | 57 | /** 58 | * 获取view对应的AbstractThemeWidget。 59 | * 60 | * @param view View. 61 | * @return AbstractThemeWidget 62 | */ 63 | public static AbstractThemeWidget getThemeWidget(View view) { 64 | checkInstance(); 65 | return sThemeManager.getThemeWidget(view.getClass()); 66 | } 67 | 68 | /** 69 | * 获取Class对应的AbstractThemeWidget。 70 | * 71 | * @param clazz class of View. 72 | * @return AbstractThemeWidget 73 | */ 74 | public static AbstractThemeWidget getThemeWidget(Class clazz) { 75 | checkInstance(); 76 | return sThemeManager.getThemeWidget(clazz); 77 | } 78 | 79 | /** 80 | * 添加主题改变的观察器 81 | * 82 | * @param themeObserver 83 | */ 84 | public static void addObserver(IThemeObserver themeObserver) { 85 | checkInstance(); 86 | sThemeManager.addObserver(themeObserver); 87 | } 88 | 89 | /** 90 | * 移除主题改变的观察器 91 | * 92 | * @param themeObserver 93 | */ 94 | public static void removeObserver(IThemeObserver themeObserver) { 95 | checkInstance(); 96 | sThemeManager.removeObserver(themeObserver); 97 | } 98 | 99 | /** 100 | * 组装主题相关元素,注意在Activity的setContentView之前调用。 101 | * 102 | * @param activity AppCompatActivity 103 | */ 104 | public static void assembleThemeBeforeInflate(AppCompatActivity activity) { 105 | checkInstance(); 106 | sThemeManager.assembleThemeBeforeInflate(activity); 107 | } 108 | 109 | /** 110 | * 动态地为View添加主题控件的WidgetKey。 111 | * 112 | * @param view 113 | * @return AbstractThemeWidget 114 | */ 115 | public static AbstractThemeWidget addViewThemeWidgetKeyTag(View view) { 116 | checkInstance(); 117 | return sThemeManager.addViewThemeWidgetKeyTag(view); 118 | } 119 | 120 | /** 121 | * 判断是否开启了暗黑模式 122 | * @return true or false 123 | */ 124 | public static DarkMode getDarkMode() { 125 | checkInstance(); 126 | return sThemeManager.getDarkMode(); 127 | } 128 | 129 | public static void setDarkMode(DarkMode mode) { 130 | checkInstance(); 131 | sThemeManager.setDarkMode(mode); 132 | } 133 | 134 | /** 135 | * 判断系统是否开启了暗黑模式 136 | * @return true or false 137 | */ 138 | public static boolean isSystemDarkMode() { 139 | checkInstance(); 140 | return sThemeManager.isSystemDarkMode(); 141 | } 142 | 143 | /** 144 | * 改变主题 145 | * 146 | * @param whichTheme 应用主题,这里对应的是ActivityTheme中的theme[]的索引值 147 | * @return true 表示切换主题成功,反之为false. 148 | */ 149 | public static boolean setAppTheme(int whichTheme) { 150 | checkInstance(); 151 | return sThemeManager.setAppTheme(whichTheme); 152 | } 153 | 154 | /** 155 | * 设置应用的默认主题,修改后就会这个值就不再生效了,一般在Application的子类中设置 156 | * 157 | * @param defaultThemeIndex 默认主题资源数组所索引 158 | */ 159 | public static void setDefaultAppTheme(int defaultThemeIndex) { 160 | checkInstance(); 161 | sDefaultThemeIndex = defaultThemeIndex; 162 | sThemeManager.setDefaultTheme(sDefaultThemeIndex); 163 | } 164 | 165 | /** 166 | * 获取应用的当前主题 167 | * 168 | * @return int 当前应用的theme[]的索引值 169 | */ 170 | public static int getAppTheme() { 171 | checkInstance(); 172 | return sThemeManager.getAppTheme(); 173 | } 174 | 175 | /** 176 | * 改变Activity主题 177 | * 178 | * @param activity 179 | * @param theme 180 | */ 181 | static void applyTheme(Activity activity, @StyleRes int theme) { 182 | checkInstance(); 183 | sThemeManager.applyTheme(activity, theme); 184 | } 185 | 186 | /** 187 | * 改变主题后,动态的应用View的主题改变,通常情况只在DecorView中找不到这些view的情况下使用。 188 | * 189 | * @param view 190 | */ 191 | public static void applyTheme(View view) { 192 | checkInstance(); 193 | sThemeManager.applyTheme(view); 194 | } 195 | 196 | /** 197 | * 对View的单个Attr属性设定主题,可用来动态修改主题元素的索引值 198 | * 199 | * @param view view 200 | * @param themeElementKey Attr属性值 201 | * @param themeElementValue Attr属性值 202 | */ 203 | public static void applySingleElementTheme(View view, @AttrRes int themeElementKey, @AttrRes int themeElementValue) { 204 | checkInstance(); 205 | AbstractThemeWidget themeWidget; 206 | if (sThemeManager.getThemeWidgetKey(view) == null) { 207 | themeWidget = addViewThemeWidgetKeyTag(view); 208 | } else { 209 | themeWidget = sThemeManager.getThemeWidget(view.getClass()); 210 | } 211 | themeWidget.applySingleElementTheme(view, themeElementKey, themeElementValue); 212 | } 213 | 214 | /** 215 | * 移除View的单个Attr属性设定的主题元素值,移除此Attr属性将不再跟随主题而改变 216 | * 217 | * @param view view 218 | * @param themeElementKey Attr属性值 219 | */ 220 | public static void removeSingleElementTheme(View view, @AttrRes int themeElementKey) { 221 | checkInstance(); 222 | AbstractThemeWidget themeWidget; 223 | if (sThemeManager.getThemeWidgetKey(view) == null) { 224 | themeWidget = addViewThemeWidgetKeyTag(view); 225 | } else { 226 | themeWidget = sThemeManager.getThemeWidget(view.getClass()); 227 | } 228 | themeWidget.removeSingleElementTheme(view, themeElementKey); 229 | } 230 | 231 | public static void release() { 232 | sThemeManager = null; 233 | } 234 | 235 | private static void checkInstance() throws IllegalStateException { 236 | if (sThemeManager == null) { 237 | throw new IllegalStateException("Do you initialize MultiTheme in creation of your app's application."); 238 | } 239 | } 240 | 241 | public static int d(String tag, String msg) { 242 | if (debugMode) { 243 | return Log.d(tag, msg); 244 | } else { 245 | return -1; 246 | } 247 | } 248 | 249 | public static int e(String tag, String msg) { 250 | if (debugMode) { 251 | return Log.e(tag, msg); 252 | } else { 253 | return -1; 254 | } 255 | } 256 | 257 | public static int i(String tag, String msg) { 258 | if (debugMode) { 259 | return Log.i(tag, msg); 260 | } else { 261 | return -1; 262 | } 263 | } 264 | } 265 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/widget/AbstractThemeWidget.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme.widget; 2 | 3 | import android.content.res.ColorStateList; 4 | import android.content.res.TypedArray; 5 | import android.graphics.drawable.Drawable; 6 | import androidx.annotation.AnyRes; 7 | import androidx.annotation.AttrRes; 8 | import androidx.annotation.ColorRes; 9 | import androidx.annotation.DrawableRes; 10 | import androidx.annotation.StyleRes; 11 | import android.util.AttributeSet; 12 | import android.util.SparseIntArray; 13 | import android.util.TypedValue; 14 | import android.view.View; 15 | 16 | import java.util.HashSet; 17 | import java.util.Set; 18 | 19 | import io.github.leonhover.theme.MultiTheme; 20 | import io.github.leonhover.theme.R; 21 | import io.github.leonhover.theme.ThemeUtils; 22 | import io.github.leonhover.theme.annotation.MultiThemeAttrs; 23 | 24 | import static io.github.leonhover.theme.ThemeUtils.getAttrResId; 25 | import static io.github.leonhover.theme.ThemeUtils.isAttrReference; 26 | 27 | /** 28 | * Created by leonhover on 16-9-27. 29 | */ 30 | 31 | public abstract class AbstractThemeWidget implements IThemeWidget { 32 | 33 | public static final String TAG = AbstractThemeWidget.class.getSimpleName(); 34 | 35 | /** 36 | * 主题元素集合 37 | */ 38 | private Set themeElementKeySet = new HashSet<>(); 39 | 40 | public AbstractThemeWidget() { 41 | initializeAppThemeElements(this.getClass()); 42 | initializeLibraryElements(); 43 | } 44 | 45 | /** 46 | * 初始化应用或FrameWork的attr属性作为Key的主题元素 47 | */ 48 | private void initializeAppThemeElements(Class themeWidgetClass) { 49 | if (themeWidgetClass != null && AbstractThemeWidget.class.isAssignableFrom(themeWidgetClass)) { 50 | Class superClass = themeWidgetClass.getSuperclass(); 51 | initializeAppThemeElements(superClass); 52 | 53 | MultiThemeAttrs multiThemeAttrs = themeWidgetClass.getAnnotation(MultiThemeAttrs.class); 54 | if (multiThemeAttrs != null) { 55 | for (int attrRes : multiThemeAttrs.value()) { 56 | themeElementKeySet.add(attrRes); 57 | } 58 | } 59 | } 60 | } 61 | 62 | /** 63 | * 仅供MultiThemeLibrary来初始一些不能直接当做常量引用的attr属性主题元素Key。 64 | */ 65 | protected void initializeLibraryElements() { 66 | 67 | } 68 | 69 | protected final void addThemeElementKey(@AttrRes int themeElementKey) { 70 | themeElementKeySet.add(themeElementKey); 71 | } 72 | 73 | /** 74 | * 添加主题支持的Attribute Resource Id; 75 | * 76 | * @param themeElementKey Attribute Ressurce Id 77 | */ 78 | public final void add(@AttrRes int themeElementKey) { 79 | themeElementKeySet.add(themeElementKey); 80 | } 81 | 82 | @Override 83 | public void assemble(View view, AttributeSet attributeSet) { 84 | MultiTheme.d(TAG, "assemble"); 85 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 86 | return; 87 | } 88 | 89 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 90 | 91 | int count = attributeSet.getAttributeCount(); 92 | 93 | for (int themeElementKey : themeElementKeySet) { 94 | for (int i = 0; i < count; i++) { 95 | if (themeElementKey == attributeSet.getAttributeNameResource(i)) { 96 | String attrValue = attributeSet.getAttributeValue(i); 97 | int attrId = -1; 98 | if (isAttrReference(attrValue)) { 99 | attrId = getAttrResId(attrValue); 100 | themeElementPairs.put(themeElementKey, attrId); 101 | break; 102 | } 103 | } 104 | } 105 | } 106 | 107 | } 108 | 109 | @Override 110 | public void applyTheme(View view) { 111 | MultiTheme.d(TAG, "applyTheme"); 112 | if (view == null) { 113 | throw new IllegalArgumentException(" view is illegal!!"); 114 | } 115 | 116 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 117 | return; 118 | } 119 | 120 | SparseIntArray themeElements = getThemeElementPairs(view); 121 | 122 | for (int i = 0; i < themeElements.size(); i++) { 123 | int attrResSupported = themeElements.keyAt(i); 124 | int attrResId = themeElements.get(attrResSupported); 125 | 126 | if (attrResId > 0) { 127 | applySingleElementTheme(view, attrResSupported, attrResId); 128 | } 129 | } 130 | } 131 | 132 | @Override 133 | public void applyStyle(View view, @StyleRes int styleRes) { 134 | 135 | if (view == null) { 136 | throw new IllegalArgumentException(" view is illegal!!"); 137 | } 138 | 139 | if (themeElementKeySet == null || themeElementKeySet.size() < 1) { 140 | return; 141 | } 142 | 143 | for (Integer themeElementKey : themeElementKeySet) { 144 | 145 | TypedArray typedArray = view.getContext().obtainStyledAttributes(styleRes, new int[]{themeElementKey}); 146 | if (typedArray != null && typedArray.getIndexCount() > 0) { 147 | TypedValue typedValue = new TypedValue(); 148 | typedArray.getValue(0, typedValue); 149 | if (typedValue.resourceId > 0) { 150 | applyElementTheme(view, themeElementKey, typedValue.resourceId); 151 | } 152 | } 153 | 154 | if (typedArray != null) { 155 | typedArray.recycle(); 156 | } 157 | } 158 | } 159 | 160 | /** 161 | * 应用单个主题元素 162 | * 163 | * @param view View 164 | * @param themeElementKey 主题元素Key 165 | * @param themeElementValue Attr资源ID 166 | */ 167 | public void applySingleElementTheme(View view, @AttrRes int themeElementKey, @AttrRes int themeElementValue) { 168 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 169 | themeElementPairs.put(themeElementKey, themeElementValue); 170 | TypedValue typedValue = new TypedValue(); 171 | view.getContext().getTheme().resolveAttribute(themeElementValue, typedValue, true); 172 | if (typedValue.resourceId > 0) { 173 | applyElementTheme(view, themeElementKey, typedValue.resourceId); 174 | } 175 | } 176 | 177 | /** 178 | * 移除View的单个主题元素,移除的主题元素将不再跟随主题变化 179 | * 180 | * @param view View 181 | * @param themeElementKey 主题元素Key 182 | */ 183 | public void removeSingleElementTheme(View view, @AttrRes int themeElementKey) { 184 | SparseIntArray themeElementPairs = getThemeElementPairs(view); 185 | int index = themeElementPairs.indexOfKey(themeElementKey); 186 | if (index >= 0) { 187 | themeElementPairs.removeAt(index); 188 | } 189 | } 190 | 191 | /** 192 | * 应用单个主题元素 193 | * 194 | * @param view View 195 | * @param themeElementKey 主题元素 196 | * @param resId 资源ID 197 | */ 198 | protected void applyElementTheme(View view, @AttrRes int themeElementKey, @AnyRes int resId) { 199 | 200 | } 201 | 202 | /** 203 | * 获取单个View的所有主题元素键值对 204 | * 205 | * @param view View 206 | * @return SparseIntArray 207 | */ 208 | private static SparseIntArray getThemeElementPairs(View view) { 209 | 210 | SparseIntArray themeElementPairs = (SparseIntArray) view.getTag(R.id.amt_tag_theme_element_pairs); 211 | 212 | if (themeElementPairs == null) { 213 | themeElementPairs = new SparseIntArray(); 214 | view.setTag(R.id.amt_tag_theme_element_pairs, themeElementPairs); 215 | } 216 | 217 | return themeElementPairs; 218 | } 219 | 220 | public static Drawable getDrawable(View view, @DrawableRes int drawableResId) { 221 | return ThemeUtils.getDrawableWithResId(view.getContext(), drawableResId); 222 | } 223 | 224 | public static int getColor(View view, @ColorRes int colorResId) { 225 | return ThemeUtils.getColorWithResId(view.getContext(), colorResId); 226 | } 227 | 228 | public static ColorStateList getColorStateList(View view, @ColorRes int colorStateListResId) { 229 | return ThemeUtils.getColorStateListWithResId(view.getContext(), colorStateListResId); 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeUtils.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.annotation.SuppressLint; 4 | import android.content.Context; 5 | import android.content.res.ColorStateList; 6 | import android.content.res.Configuration; 7 | import android.content.res.Resources; 8 | import android.graphics.drawable.Drawable; 9 | import android.os.Build; 10 | import androidx.annotation.AttrRes; 11 | import androidx.annotation.ColorRes; 12 | import androidx.annotation.DrawableRes; 13 | import androidx.annotation.IdRes; 14 | import android.text.TextUtils; 15 | import android.util.TypedValue; 16 | import android.view.View; 17 | 18 | import java.lang.reflect.Field; 19 | import java.lang.reflect.InvocationTargetException; 20 | import java.lang.reflect.Method; 21 | 22 | /** 23 | * Created by leonhover on 16-9-26. 24 | */ 25 | 26 | public class ThemeUtils { 27 | 28 | public static final boolean IS_JELLY_BEAN = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN; 29 | public static final boolean IS_KITKAT = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; 30 | public static final boolean IS_LOLLIPOP = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP; 31 | public static final boolean IS_M = Build.VERSION.SDK_INT >= Build.VERSION_CODES.M; 32 | 33 | /** 34 | * 判断是否是以“?attr/**”引用的资源 35 | * 36 | * @param attrValue AttributeValue 37 | * @return true or false 38 | */ 39 | public static boolean isAttrReference(String attrValue) { 40 | if (!TextUtils.isEmpty(attrValue) && attrValue.startsWith("?")) { 41 | return true; 42 | } 43 | 44 | return false; 45 | } 46 | 47 | /** 48 | * 获取Attr的资源ID 49 | * 50 | * @param attrValue AttributeValue 51 | * @return resid 52 | */ 53 | public static int getAttrResId(String attrValue) { 54 | if (TextUtils.isEmpty(attrValue)) { 55 | return -1; 56 | } 57 | 58 | String resIdStr = attrValue.substring(1); 59 | return Integer.valueOf(resIdStr); 60 | } 61 | 62 | /** 63 | * 获取attrResId指向的颜色Color 64 | * 65 | * @param context 66 | * @param attrResId attr资源id 67 | * @return Color 68 | */ 69 | public static int getColor(Context context, @AttrRes int attrResId) { 70 | if (context == null) { 71 | return -1; 72 | } 73 | return getColor(context.getTheme(), context.getResources(), attrResId); 74 | } 75 | 76 | /** 77 | * 获取attrResId指向的颜色Color 78 | * 79 | * @param theme 主题 80 | * @param resources 资源 81 | * @param attrResId attr资源id 82 | * @return Color 83 | */ 84 | @SuppressWarnings("NewApi") 85 | public static int getColor(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 86 | TypedValue typedValue = new TypedValue(); 87 | theme.resolveAttribute(attrResId, typedValue, true); 88 | MultiTheme.d("a", "Color type:" + typedValue.toString()); 89 | int color; 90 | if (IS_M) { 91 | color = resources.getColor(typedValue.resourceId, theme); 92 | } else { 93 | color = resources.getColor(typedValue.resourceId); 94 | } 95 | return color; 96 | } 97 | 98 | 99 | /** 100 | * 获取colorResId指向的颜色Color 101 | * 102 | * @param context 103 | * @param colorResId 颜色资源id 104 | * @return Color 105 | */ 106 | public static int getColorWithResId(Context context, @ColorRes int colorResId) { 107 | if (context == null) { 108 | return -1; 109 | } 110 | return getColorWithResId(context.getTheme(), context.getResources(), colorResId); 111 | } 112 | 113 | /** 114 | * 获取colorResId指向的颜色Color 115 | * 116 | * @param theme 主题 117 | * @param resources 资源 118 | * @param colorResId 颜色资源id 119 | * @return Color 120 | */ 121 | @SuppressWarnings("NewApi") 122 | public static int getColorWithResId(Resources.Theme theme, Resources resources, @ColorRes int colorResId) { 123 | int color; 124 | if (IS_M) { 125 | color = resources.getColor(colorResId, theme); 126 | } else { 127 | color = resources.getColor(colorResId); 128 | } 129 | return color; 130 | } 131 | 132 | 133 | /** 134 | * 获取attrResId指向的颜色ColorStateList 135 | * 136 | * @param context 137 | * @param attrResId attr资源id 138 | * @return ColorStateList 139 | */ 140 | public static ColorStateList getColorStateList(Context context, @AttrRes int attrResId) { 141 | if (context == null) { 142 | return null; 143 | } 144 | return getColorStateList(context.getTheme(), context.getResources(), attrResId); 145 | } 146 | 147 | /** 148 | * 获取attrResId指向的颜色ColorStateList 149 | * 150 | * @param theme 主题 151 | * @param resources 资源 152 | * @param attrResId attr资源id 153 | * @return ColorStateList 154 | */ 155 | @SuppressWarnings("NewApi") 156 | public static ColorStateList getColorStateList(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 157 | TypedValue typedValue = new TypedValue(); 158 | theme.resolveAttribute(attrResId, typedValue, true); 159 | 160 | MultiTheme.d("a", "ColorStateList type:" + typedValue.toString()); 161 | ColorStateList colorStateList; 162 | 163 | if (IS_M) { 164 | colorStateList = resources.getColorStateList(typedValue.resourceId, theme); 165 | } else { 166 | colorStateList = resources.getColorStateList(typedValue.resourceId); 167 | } 168 | return colorStateList; 169 | } 170 | 171 | /** 172 | * 获取ColorStateListResId指向的颜色ColorStateList 173 | * 174 | * @param context 175 | * @param colorStateListResId 资源id 176 | * @return ColorStateList 177 | */ 178 | public static ColorStateList getColorStateListWithResId(Context context, @ColorRes int colorStateListResId) { 179 | if (context == null) { 180 | return null; 181 | } 182 | return getColorStateListWithResId(context.getTheme(), context.getResources(), colorStateListResId); 183 | } 184 | 185 | 186 | /** 187 | * 获取ColorStateListResId指向的颜色ColorStateList 188 | * 189 | * @param theme 主题 190 | * @param resources 资源 191 | * @param colorStateListResId 资源id 192 | * @return ColorStateList 193 | */ 194 | @SuppressWarnings("NewApi") 195 | public static ColorStateList getColorStateListWithResId(Resources.Theme theme, Resources resources, @ColorRes int colorStateListResId) { 196 | 197 | ColorStateList colorStateList; 198 | 199 | if (IS_M) { 200 | colorStateList = resources.getColorStateList(colorStateListResId, theme); 201 | } else { 202 | colorStateList = resources.getColorStateList(colorStateListResId); 203 | } 204 | return colorStateList; 205 | } 206 | 207 | /** 208 | * 获取attrResId指向的Drawable 209 | * 210 | * @param context 211 | * @param attrResId attr资源id 212 | * @return Drawable 213 | */ 214 | public static Drawable getDrawable(Context context, @AttrRes int attrResId) { 215 | if (context == null) { 216 | return null; 217 | } 218 | 219 | return getDrawable(context.getTheme(), context.getResources(), attrResId); 220 | } 221 | 222 | /** 223 | * 获取attrResId指向的Drawable 224 | * 225 | * @param theme 主题 226 | * @param resources 资源 227 | * @param attrResId attr资源id 228 | * @return Drawable 229 | */ 230 | @SuppressWarnings("NewApi") 231 | public static Drawable getDrawable(Resources.Theme theme, Resources resources, @AttrRes int attrResId) { 232 | TypedValue typedValue = new TypedValue(); 233 | theme.resolveAttribute(attrResId, typedValue, true); 234 | Drawable drawable; 235 | MultiTheme.d("a", "drawable type:" + typedValue.toString()); 236 | if (IS_M) { 237 | drawable = resources.getDrawable(typedValue.resourceId, theme); 238 | } else { 239 | drawable = resources.getDrawable(typedValue.resourceId); 240 | } 241 | return drawable; 242 | } 243 | 244 | 245 | /** 246 | * 获取DrawableResId指向的Drawable 247 | * 248 | * @param context 249 | * @param drawableResId Drawable资源id 250 | * @return Drawable 251 | */ 252 | public static Drawable getDrawableWithResId(Context context, @DrawableRes int drawableResId) { 253 | if (context == null) { 254 | return null; 255 | } 256 | 257 | return getDrawableWithResId(context.getTheme(), context.getResources(), drawableResId); 258 | } 259 | 260 | /** 261 | * 获取DrawableResId指向的Drawable 262 | * 263 | * @param theme 主题 264 | * @param resources 资源 265 | * @param drawableResId Drawable资源id 266 | * @return Drawable 267 | */ 268 | @SuppressLint("NewApi") 269 | public static Drawable getDrawableWithResId(Resources.Theme theme, Resources resources, @DrawableRes int drawableResId) { 270 | Drawable drawable; 271 | if (IS_M) { 272 | drawable = resources.getDrawable(drawableResId, theme); 273 | } else { 274 | drawable = resources.getDrawable(drawableResId); 275 | } 276 | return drawable; 277 | } 278 | 279 | public static T getViewTag(View view, @IdRes int tagKey) throws ClassCastException, NullPointerException { 280 | try { 281 | return (T) view.getTag(tagKey); 282 | } catch (ClassCastException e) { 283 | throw e; 284 | } catch (NullPointerException e) { 285 | throw e; 286 | } 287 | } 288 | 289 | /** 290 | * 获取StatusBar高度 291 | * 292 | * @param context 293 | * @return 294 | */ 295 | @SuppressLint("PrivateApi") 296 | public static int getStatusBarHeight(Context context) { 297 | Class c = null; 298 | Object obj = null; 299 | Field field = null; 300 | int x = 0, statusBarHeight = 0; 301 | try { 302 | c = Class.forName("com.android.internal.R$dimen"); 303 | obj = c.newInstance(); 304 | field = c.getField("status_bar_height"); 305 | x = Integer.parseInt(field.get(obj).toString()); 306 | statusBarHeight = context.getResources().getDimensionPixelSize(x); 307 | } catch (Exception e1) { 308 | e1.printStackTrace(); 309 | } 310 | return statusBarHeight; 311 | } 312 | 313 | public static Object invokeMethod(Object obj, String method, Object... parameters) { 314 | if (obj == null || TextUtils.isEmpty(method)) { 315 | return null; 316 | } 317 | 318 | Class[] parameterTypes = new Class[parameters.length]; 319 | for (int i = 0; i < parameters.length; i++) { 320 | parameterTypes[i] = parameters[i].getClass(); 321 | } 322 | 323 | try { 324 | Method methodWanted = obj.getClass().getDeclaredMethod(method, parameterTypes); 325 | methodWanted.setAccessible(true); 326 | return methodWanted.invoke(obj, parameters); 327 | } catch (NoSuchMethodException e) { 328 | e.printStackTrace(); 329 | } catch (InvocationTargetException e) { 330 | e.printStackTrace(); 331 | } catch (IllegalAccessException e) { 332 | e.printStackTrace(); 333 | } 334 | 335 | return null; 336 | } 337 | 338 | /** 339 | * 判断是否是暗黑模式开启状态 340 | * 341 | * @param context Context 342 | * @return true or false 343 | */ 344 | public static boolean isSystemDarkMode(Context context) { 345 | int mode = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK; 346 | boolean result = mode == Configuration.UI_MODE_NIGHT_YES; 347 | MultiTheme.d(MultiTheme.TAG, "isSystemDarkMode result:" + result); 348 | return result; 349 | } 350 | } 351 | -------------------------------------------------------------------------------- /AndroidMultiTheme/android-multi-theme/src/main/java/io/github/leonhover/theme/ThemeManager.java: -------------------------------------------------------------------------------- 1 | package io.github.leonhover.theme; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import androidx.annotation.StyleRes; 7 | import androidx.core.view.LayoutInflaterCompat; 8 | import androidx.appcompat.app.AppCompatActivity; 9 | import androidx.appcompat.app.AppCompatDelegate; 10 | import androidx.appcompat.widget.Toolbar; 11 | import android.util.AttributeSet; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.AbsListView; 16 | import android.widget.CompoundButton; 17 | import android.widget.ImageView; 18 | import android.widget.LinearLayout; 19 | import android.widget.ListView; 20 | import android.widget.ProgressBar; 21 | import android.widget.SeekBar; 22 | import android.widget.TextView; 23 | 24 | import java.util.HashMap; 25 | import java.util.Iterator; 26 | import java.util.Map; 27 | import java.util.Set; 28 | import java.util.TreeSet; 29 | 30 | import io.github.leonhover.theme.base.widget.CoverImageView; 31 | import io.github.leonhover.theme.custom.CoverImageWidget; 32 | import io.github.leonhover.theme.widget.AbsListViewWidget; 33 | import io.github.leonhover.theme.widget.AbstractThemeWidget; 34 | import io.github.leonhover.theme.widget.CompoundButtonWidget; 35 | import io.github.leonhover.theme.widget.IThemeWidget; 36 | import io.github.leonhover.theme.widget.ImageViewWidget; 37 | import io.github.leonhover.theme.widget.LinearLayoutWidget; 38 | import io.github.leonhover.theme.widget.ListViewWidget; 39 | import io.github.leonhover.theme.widget.ProgressBarWidget; 40 | import io.github.leonhover.theme.widget.SeekBarWidget; 41 | import io.github.leonhover.theme.widget.TextViewWidget; 42 | import io.github.leonhover.theme.widget.ToolBarWidget; 43 | import io.github.leonhover.theme.widget.ViewWidget; 44 | 45 | 46 | /** 47 | * Created by leonhover on 16-9-26. 48 | */ 49 | 50 | class ThemeManager { 51 | 52 | private static final String TAG = ThemeManager.class.getSimpleName(); 53 | 54 | private Map, AbstractThemeWidget> themeWidgetMap; 55 | 56 | private ThemeViewCreator themeViewCreator; 57 | 58 | private int appTheme = -1; 59 | 60 | private Application application; 61 | 62 | private Set themeObserverSet; 63 | 64 | ThemeManager(Application application) { 65 | this.application = application; 66 | this.themeWidgetMap = new HashMap<>(); 67 | this.themeWidgetMap.put(View.class, new ViewWidget()); 68 | this.themeWidgetMap.put(TextView.class, new TextViewWidget()); 69 | this.themeWidgetMap.put(ImageView.class, new ImageViewWidget()); 70 | this.themeWidgetMap.put(CompoundButton.class, new CompoundButtonWidget()); 71 | this.themeWidgetMap.put(ProgressBar.class, new ProgressBarWidget()); 72 | this.themeWidgetMap.put(ListView.class, new ListViewWidget()); 73 | this.themeWidgetMap.put(SeekBar.class, new SeekBarWidget()); 74 | this.themeWidgetMap.put(LinearLayout.class, new LinearLayoutWidget()); 75 | this.themeWidgetMap.put(AbsListView.class, new AbsListViewWidget()); 76 | this.themeWidgetMap.put(Toolbar.class, new ToolBarWidget()); 77 | this.themeWidgetMap.put(CoverImageView.class, new CoverImageWidget()); 78 | 79 | this.themeObserverSet = new TreeSet<>(); 80 | this.themeViewCreator = new ThemeViewCreator(); 81 | this.appTheme = ThemePreferences.getAppTheme(this.application); 82 | 83 | } 84 | 85 | /** 86 | * 判断系统是否是暗黑模式 87 | * 88 | * @return true or false 89 | */ 90 | boolean isSystemDarkMode() { 91 | return ThemeUtils.isSystemDarkMode(this.application); 92 | } 93 | 94 | /** 95 | * 获取设置的暗黑模式 96 | * 97 | * @return true or false 98 | */ 99 | DarkMode getDarkMode() { 100 | return ThemePreferences.getDarkMode(this.application); 101 | } 102 | 103 | /** 104 | * 设置跟跟随系统颜色模式调整主题 105 | * 106 | * @param mode 跟随与否 107 | */ 108 | void setDarkMode(DarkMode mode) { 109 | switch (mode) { 110 | case on: 111 | setAppTheme(MultiTheme.DARK_THEME); 112 | break; 113 | case followSystem: 114 | if (isSystemDarkMode()) { 115 | setAppTheme(MultiTheme.DARK_THEME); 116 | } else { 117 | setAppTheme(MultiTheme.sDefaultThemeIndex); 118 | } 119 | break; 120 | default: 121 | setAppTheme(MultiTheme.sDefaultThemeIndex); 122 | } 123 | ThemePreferences.setDarkMode(this.application, mode); 124 | } 125 | 126 | void addThemeWidget(Class widgetKey, AbstractThemeWidget themeWidget) { 127 | this.themeWidgetMap.put(widgetKey, themeWidget); 128 | } 129 | 130 | AbstractThemeWidget getThemeWidget(Class clazz) { 131 | Class widgetKey = findProperThemeWidgetKey(clazz); 132 | return this.themeWidgetMap.get(widgetKey); 133 | } 134 | 135 | void addObserver(IThemeObserver observer) { 136 | this.themeObserverSet.add(observer); 137 | } 138 | 139 | void removeObserver(IThemeObserver observer) { 140 | Iterator obsIterator = this.themeObserverSet.iterator(); 141 | while (obsIterator.hasNext()) { 142 | IThemeObserver obs = obsIterator.next(); 143 | if (observer.equals(obs)) { 144 | obsIterator.remove(); 145 | return; 146 | } 147 | } 148 | } 149 | 150 | void assembleThemeBeforeInflate(final AppCompatActivity activity) { 151 | 152 | if (activity == null) { 153 | throw new NullPointerException(); 154 | } 155 | LayoutInflaterCompat.setFactory2(activity.getLayoutInflater(), new LayoutInflater.Factory2() { 156 | @Override 157 | public View onCreateView(String name, Context context, AttributeSet attributeSet) { 158 | MultiTheme.d(TAG, "onCreateView name:" + name); 159 | return onCreateView(null, name, context, attributeSet); 160 | } 161 | 162 | @Override 163 | public View onCreateView(View parent, String name, Context context, AttributeSet attrs) { 164 | MultiTheme.d(TAG, "onCreateView parent:" + parent + ",name:" + name); 165 | AppCompatDelegate appCompatDelegate = activity.getDelegate(); 166 | View view = appCompatDelegate.createView(parent, name, context, attrs); 167 | 168 | 169 | if (view == null) { 170 | view = themeViewCreator.createView(parent, name, context, attrs); 171 | } 172 | 173 | if (view == null) { 174 | return null; 175 | } 176 | 177 | Class widgetKey = findProperThemeWidgetKey(view.getClass()); 178 | assembleViewThemeElement(view, attrs, widgetKey); 179 | 180 | return view; 181 | } 182 | }); 183 | } 184 | 185 | private Class findProperThemeWidgetKey(Class clazz) { 186 | 187 | Class tmpKey = null; 188 | for (Class widgetKey : this.themeWidgetMap.keySet()) { 189 | 190 | if (clazz.equals(widgetKey)) { 191 | return clazz; 192 | } 193 | 194 | if (widgetKey.isAssignableFrom(clazz)) { 195 | if (tmpKey == null) { 196 | tmpKey = widgetKey; 197 | } else { 198 | if (tmpKey.isAssignableFrom(widgetKey)) { 199 | tmpKey = widgetKey; 200 | } 201 | } 202 | } 203 | } 204 | 205 | if (tmpKey == null) { 206 | return null; 207 | } else { 208 | return tmpKey; 209 | } 210 | 211 | } 212 | 213 | /** 214 | * 指定View的主题WidgetKey 215 | * 216 | * @param view 217 | * @return AbstractThemeWidget 218 | */ 219 | AbstractThemeWidget addViewThemeWidgetKeyTag(View view) { 220 | if (view != null) { 221 | Class widgetKey = findProperThemeWidgetKey(view.getClass()); 222 | view.setTag(R.id.amt_tag_widget_key, widgetKey); 223 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 224 | return this.themeWidgetMap.get(widgetKey); 225 | } 226 | return null; 227 | } 228 | 229 | /** 230 | * 组装每个View的主题元素 231 | */ 232 | private void assembleViewThemeElement(View view, AttributeSet attributeSet, Class widgetKey) { 233 | 234 | MultiTheme.d(TAG, "assembleViewThemeElement theme widget type:" + widgetKey + ", view:" + view); 235 | if (view == null) { 236 | return; 237 | } 238 | 239 | IThemeWidget themeWidget = this.themeWidgetMap.get(widgetKey); 240 | if (themeWidget != null) { 241 | view.setTag(R.id.amt_tag_widget_key, widgetKey); 242 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 243 | if (attributeSet != null) { 244 | int styleResId = attributeSet.getStyleAttribute(); 245 | if (styleResId != 0) { 246 | view.setTag(R.id.amt_tag_widget_style, styleResId); 247 | } 248 | themeWidget.assemble(view, attributeSet); 249 | } 250 | MultiTheme.d(TAG, "assembleViewThemeElement theme widget type: " + widgetKey + " themeWidget:" + themeWidget.getClass().getSimpleName()); 251 | } else { 252 | view.setTag(R.id.amt_tag_widget_key, null); 253 | MultiTheme.i(TAG, "unsupported theme widget type " + widgetKey + ",is your custom theme widget?"); 254 | } 255 | } 256 | 257 | /** 258 | * 改变主题,在默认主题与夜间主题之间进行切换 259 | * 260 | * @return true 改变为夜间主题,false 改变为默认主题 261 | */ 262 | boolean setAppTheme(int whichTheme) { 263 | MultiTheme.d(MultiTheme.TAG, "setAppTheme whichTheme=" + whichTheme); 264 | 265 | if (whichTheme > -1 && whichTheme != appTheme) { 266 | ThemePreferences.setAppTheme(this.application, whichTheme); 267 | appTheme = whichTheme; 268 | for (IThemeObserver themeObserver : themeObserverSet) { 269 | themeObserver.onThemeChanged(whichTheme); 270 | } 271 | return true; 272 | } else { 273 | return false; 274 | } 275 | 276 | } 277 | 278 | int getAppTheme() { 279 | return this.appTheme; 280 | } 281 | 282 | void setDefaultTheme(int defaultThemeIndex) { 283 | if (ThemePreferences.getAppTheme(this.application) == -1) { 284 | setAppTheme(defaultThemeIndex); 285 | } 286 | } 287 | 288 | /** 289 | * 改变主题,在默认主题与夜间主题之间进行切换 290 | * 291 | * @param activity Activity 292 | * @param themeResId 主题资源id 293 | */ 294 | void applyTheme(Activity activity, @StyleRes int themeResId) { 295 | activity.setTheme(themeResId); 296 | applyTheme(activity); 297 | } 298 | 299 | private void applyTheme(Activity activity) { 300 | MultiTheme.d(TAG, "applyThemeForActivity"); 301 | if (activity == null) { 302 | throw new IllegalArgumentException("activity is null!"); 303 | } 304 | 305 | View decorView = activity.getWindow().getDecorView(); 306 | applyTheme(decorView); 307 | 308 | } 309 | 310 | /** 311 | * 对单个View应用主题 312 | * 313 | * @param view View 314 | */ 315 | void applyTheme(View view) { 316 | 317 | int themeOfView = -1; 318 | int styleResOfView = -1; 319 | try { 320 | themeOfView = ThemeUtils.getViewTag(view, R.id.amt_tag_view_current_theme); 321 | styleResOfView = ThemeUtils.getViewTag(view, R.id.amt_tag_widget_style); 322 | } catch (ClassCastException | NullPointerException e) { 323 | } 324 | 325 | if (themeOfView == appTheme) { 326 | return; 327 | } 328 | 329 | Class widgetKey = getThemeWidgetKey(view); 330 | 331 | MultiTheme.d(TAG, "applyTheme theme widget type:" + widgetKey + " ,view:" + view); 332 | IThemeWidget themeWidget = this.themeWidgetMap.get(widgetKey); 333 | if (themeWidget != null) { 334 | if (styleResOfView != -1) { 335 | themeWidget.applyStyle(view, styleResOfView); 336 | } 337 | themeWidget.applyTheme(view); 338 | MultiTheme.d(TAG, "applyTheme theme widget type: " + widgetKey + " ,view:" + view + " themeWidget:" + themeWidget); 339 | } else { 340 | MultiTheme.i(TAG, "applyTheme unsupport theme widget type:" + widgetKey + ", view:" + view); 341 | } 342 | 343 | if (view instanceof ViewGroup) { 344 | ViewGroup viewGroup = (ViewGroup) view; 345 | for (int i = 0; i < viewGroup.getChildCount(); i++) { 346 | View child = viewGroup.getChildAt(i); 347 | applyTheme(child); 348 | } 349 | } 350 | 351 | view.setTag(R.id.amt_tag_view_current_theme, getAppTheme()); 352 | } 353 | 354 | @SuppressWarnings("unchecked") 355 | Class getThemeWidgetKey(View view) { 356 | return (Class) view.getTag(R.id.amt_tag_widget_key); 357 | } 358 | } 359 | --------------------------------------------------------------------------------