最新消息:念念不忘,必有回响。

多主题

android开发 半白色 968浏览 0评论

MagicaSakura

MagicaSakura 是 Android 多主题框架。~ is an Android multi theme library which supporting both daily colorful theme and night theme.

cover

MagicaSakura is an Android multi theme library which supporting both daily colorful theme and night theme.

magicasakura是一个Android的多主题库,支持日常的丰富多彩的日间主题和夜间主题。

 

Feature

  1. Support both the daily colorful theme and the night theme. 多种主题,包括白天主体和夜间主题。
  2. Switch different theme without recreating activities. 主题切换不用重新开启activity。
  3. Provide TintXXX Widgets for adapting multi theme to be more convenient and fast. 提供TintXXX Widgets,使多主题更方便快捷。
  4. Just writie a drawable.xml or layout.xml can be automatically adapted to different theme styles. 只需编写一个drawable.xml或layout.xml即可自动适应不同的主题风格。
  5. Offer backward-compatible versions of the Android system that can be used with 4.0.3 or higher. 提供可与4.0.3或更高版本一起使用的Android系统的向后兼容版本。
  6. Support Vector Drawable with appcompat-v7. 支持向量绘制与appcompat-v7。
  7. Easy to integrate to your app. 易于集成到您的应用程序。

ScreenShot.gif

Gradle Dependency

Maven Dependency

Usage

STEP1 :

Define your app global theme color variates in values/color.xml, like as:

Must use these color variates in layout xml , color xml or drawable xml when these xml files need to be automatically adapted to different theme styles. If you use direct color value or other color variates, adapting different theme styles will be out of work.

STEP2 :

Implement ThemeUtils.switchColor interface in the app Applaction; You Define your own rules combining with the color variates(defining in Step 1) for switching different colors when choosing different themes.


 

STEP3 :

The library provides a series of TintXXX widgets which including most common android widgets.

When some place in your app needs to adapter multi theme, you can use these TintXXX widgets combining with the color variates(defining in Step 1) or color xml(using the color variates) or drawable xml(the color variates) , then they will be auto adapting.

Drawable Xml TintXXX widgets support common drawable xml tag, such as , , , , and etc in drawable xml.

(Note: when using not supporting drawable xml tag, just can’t be adapted to multi theme)

Specially, TintXXX widgets additional support directly tint drawable with app:drawableTint and app:drawableTintMode and set color alpha with android:alpha.

Here is an example:

Layout Xml TintXXX widgets can be tinted directly in layout xml that supporting most common android drawable attrs , such as background , src , drawableLeft, button and etc.

(Note: when tinting directly in layout xml , must use the color variates(defining in Step 1), otherwise adapting different theme styles will be out of work)

Here is an example:

Here is the table that supporting attr of TintXXX widgets:

attr tint tintMode
background backgroundTint backgroundTintMode
src imageTint imageTintMode
button compoundButtonTint compoundButtonTintMode
drawableXxx drawableXxxTint drawableXxxTintMode
progress progressTint,progressIndeterminateTint
track trackTint trackTintMode
thumb thumbTint thumbTintMode

Java code TintXXX widgets can also be tinted in java code. The way of tinting drawable is the same as android native methods.

Here is an example:

STEP4 :

The library provides utility class ThemeUtils to meet some special needs or your own custom widgets.

Utility class ThemeUtils mainly provides the method of tinting drawable and convert the color variates(defining in Step 1) with the current theme including colorStateList and color.

STEP5 :

About to support the night theme, there are two ways to choose.

  • Build night resource directories whitch are corresponding the default resource directories and put the independent night xml into corresponding directories, such as values-night/values, color-night/night …
  • Define a series of color variates both in values-night/values which are same name but different value, then you just write a xml once with using the colors variates to adapt the night theme.

STEP6 :

About to switch daily colorful theme, you can directly call the method Theme.refreshUI in main thread and this method also provides optional callback params to meet your custom needs during switching theme.

About to switch night theme, when the version of your android support library is below 23.2.0, you can call the method of ThemeUtils.updateNightMode to switch in the night and daily theme, and when the version is above 23.2.0, you can use android native method in android support library.

 

 

  • 希望各位开发者们可以在下方多多留言,一起交流开发心得。如果觉得本文对你有帮助,你可以将它们分享给你的朋友,或者给本站打赏,打赏的资金将用于网站的运营。
  • 转载请注明:半白色 » 多主题

    发表我的评论
    取消评论

    *

    表情

    Hi,您需要填写昵称和邮箱!

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址