Skinnable
Introduction: 东方证券 Android 换肤 Project
Tags:
东方证券 Android 换肤 Kotlin 实现方案
使用方法
1.引用 Library
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.Orientsec.Skinnable:SkinLibrary:1.0.0'
}
2.添加 support 仓库
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.7.10'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'com.github.Orientsec.Skinnable:SkinLibrary:1.0.0'
// 支持 appcompat
implementation 'androidx.appcompat:appcompat:1.5.0'
implementation 'com.github.Orientsec.Skinnable:SkinSupportCompat:1.0.0'
// 支持 material View
implementation 'com.google.android.material:material:1.6.1'
implementation 'com.github.Orientsec.Skinnable:SkinSupportDesign:1.0.0'
// 支持 ConstraintLayout
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'com.github.Orientsec.Skinnable:SkinSupportConstraintLayout:1.0.0'
}
在 Application.onCreate()方法中加入以下初始化代码
SkinConfig.builder()
.addInflater(SkinAppCompatViewFactory())
.addInflater(SkinConstraintViewFactory())
.addInflater(SkinMaterialViewFactory())
.build()
.initManager(this)
.loadSkin()
在支持换肤的 View 布局中加入
app:skinnable="true"
换肤方法
加载插件皮肤库
// 指定皮肤插件
SkinManager.loadSkin(SkinStrategy.Assets("skin.night"))
// 后缀加载
SkinManager.loadSkin(SkinStrategy.BuildIn("night"))
// 前缀加载
SkinManager.loadSkin(SkinStrategy.PrefixBuildIn("night"))
// 恢复应用默认皮肤
SkinManager.restoreDefaultSkin();
应用内换肤:
应用内换肤,皮肤名为: night; 新增需要换肤的资源添加后缀或者前缀。
需要换肤的资源为 R.color.windowBackgroundColor, 添加对应资源 R.color.windowBackgroundColor_night。
加载应用内皮肤:
// 后缀加载
SkinManager.loadSkin(SkinStrategy.BuildIn("night"))
// 前缀加载
SkinManager.loadSkin(SkinStrategy.PrefixBuildIn("night"))
推荐将应用内换肤相关的皮肤资源放到单独的目录中 注: 如果使用这种方式来增加换肤资源,记得在 build.gradle 中配置一下这个资源目录
sourceSets {
main {
res.srcDirs = ['src/main/res', 'src/main/res-night']
}
}
插件式换肤:
新建 Android application 工程,皮肤工程包名不能和宿主应用包名相同.
将需要换肤的资源放到 res 目录下(同名资源)
例如 APK 中窗口背景颜色为 colors.xml
<color name="background">#ffffff</color>
那么夜间模式你可以在 skin-night 工程中设置 colors.xml
<color name="background">#000000</color>
打包生成 apk, 即为皮肤包
将打包生成的 apk 文件, 重命名为'xxx.skin', 防止 apk 结尾的文件造成混淆.
加载皮肤插件
加载插件式皮肤, 将皮肤包放到 assets/skins 目录下
// 指定皮肤插件
SkinManager.loadSkin(SkinStrategy.Assets("skin.night"))