MDThemeKit :主题切换工具类 - SheTieJun/BaseKit GitHub Wiki

主题切换工具类 MDThemeKit

1. 介绍

基于material - MD的颜色系统做主题切换

可以使用下面的工具进行生成主题 https://www.figma.com/community/plugin/1034969338659738588

效果如下:

news2

比如:

如果你使用上面的颜色标准进行设计APP,后面想要切换主题,那么就可以使用这个工具类进行切换

2. 使用

1. 初始化 👍

MDThemeKit.startInit(context, listOf(
    ThemeBean(R.style.BaseTheme_MD3, "默认主题-跟随系统"),
    ThemeBean(R.style.BaseTheme_MD3, "默认主题-黑夜", AppCompatDelegate.MODE_NIGHT_YES),
    ThemeBean(R.style.BaseTheme_MD3, "默认主题-日间", AppCompatDelegate.MODE_NIGHT_NO),
    ThemeBean(R.style.Base_Theme_LF_Green, "春色宜人(GREEN)", AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY), 
    ThemeBean(R.style.Base_Theme_LF_RED, "红红火火(RED)", AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY),
))

2. 快速使用1:展示切换弹窗 👍

MDThemeKit.showChangeThemeDialog(fragmentActivity)

2. 快速使用2:代码直接设置

fun updateTheme(theme: ThemeBean) 
fun updateThemeByType(@StyleRes style: Int, name: String,@NightMode nightMode: Int = AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
MD3ThemeKit.updateThemeByType(style.BaseTheme_MD3, "默认主题-跟随系统11")

3. 其他相关方法,如果你想自定义主题切换弹窗,可以使用下面的方法

    val isDark: Boolean
    
    fun getCurrentTheme(): SingleLiveEvent<ThemeBean>  
    
    fun clearTheme()  
    
    fun getThemeList(): ArrayList<ThemeBean>  
    
    fun getThemeTitleList(): List<String>  
    
    fun getThemeByWhich(which: Int): ThemeBean  
    
    fun getThemeNameByWhich(which: Int): String  
    
    fun getNightModeByWhich(which: Int): Int  
    
    fun getIndexByTheme(theme: ThemeBean) 
    
    fun updateTheme(theme: ThemeBean) 
    
    fun updateTheme(@StyleRes style: Int, name: String,@NightMode nightMode: Int = AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY) 
    
    fun applyThemeOverlay(context: Context, @StyleRes theme: Int)  

已知问题

那就是如果APP的闪图界面使用了Theme.SplashScreen.IconBackground将无法修改, 因为postSplashScreenTheme被固定了

    <style name="App.SplashScreen" parent="Theme.SplashScreen.IconBackground">
        <item name="windowSplashScreenAnimatedIcon">@drawable/unsplash_cat1</item>
        <item name="windowSplashScreenBackground">@color/white</item>
        <item name="windowSplashScreenIconBackgroundColor">@color/white</item>

        <!--        must be set-->
        <item name="postSplashScreenTheme">@style/BaseTheme.MD3</item> 
    </style>
⚠️ **GitHub.com Fallback** ⚠️