android 换肤无非就是改变字体的颜色,控件的背景等显示效果。
下面看看SkinActivity的实现:
先上布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:text="要换肤的地方"
android:textColor="@color/white" />
<Button
android:id="@+id/btn_change_skin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:onClick="changeSkin"
android:text="换肤" />
</LinearLayout>
原来的样式:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="white">#ffffff</color>
</resources>
创建个皮肤包项目,仅颜色变一下,生成单独的apk放到sd卡中:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#ff0000</color>
<color name="white">#000000</color>
</resources>
换肤后的效果:
设计到的技术:
apk 动态加载
View的解析监听
// SkinActivity 的代码
private var skinInflateFactory = SkinInflateFactory()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
LayoutInflaterCompat.setFactory(layoutInflater, skinInflateFactory)
setContentView(R.layout.activity_skin)
}
fun changeSkin(v: View) {
SkinManager.getInstance().let {
it.context = this
it.loadSkin("skin01")
skinInflateFactory.apply()
}
}
监听View的解析过程,判断哪些控件需要更换样式:
class SkinInflateFactory : LayoutInflaterFactory {
val TAG = SkinInflateFactory::class.java.simpleName!!
val PREFIX_LIST = arrayListOf("android.widget.", "android.view.", "android.webkit.")
val skinViewList = ArrayList<SkinView>()
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
var view: View? = null
if (!name.contains(".")) {
// 系统控件
PREFIX_LIST.forEach {
val v = createView(it + name, context, attrs)
if (v != null) {
view = v!!
return@forEach
}
}
} else {
// 兼容包或自定义控件
view = createView(name, context, attrs)!!
}
val styleList = ArrayList<SkinStyle>()
(0..attrs.attributeCount - 1).forEach {
val attrName = attrs.getAttributeName(it)
val attrVal = attrs.getAttributeValue(it)
if (attrName.equals("textColor") || attrName.equals("background")) {
val refId = Integer.parseInt(attrVal.substring(1))
styleList.add(SkinStyle(attrName, refId))
}
}
if (styleList.isNotEmpty()) {
skinViewList.add(SkinView(view, styleList))
}
return view
}
companion object {
class SkinStyle(val attrName: String, val attrRefId: Int)
class SkinView(val view: View?, val styleList: List<SkinStyle>) {
fun apply() {
for (style in styleList) {
if (style.attrName.equals("textColor")) {
if (view is TextView) {
view.setTextColor(SkinManager.getInstance().getColor(style.attrRefId))
}
} else if (style.attrName.equals("background")) {
view?.setBackgroundDrawable(SkinManager.getInstance().getDrawable(style.attrRefId))
}
}
}
}
}
fun apply() {
for (view in skinViewList) {
view.apply()
}
}
private fun createView(name: String, context: Context, attrs: AttributeSet): View? {
try {
var c = Class.forName(name).getConstructor(Context::class.java, AttributeSet::class.java)
return c.newInstance(context, attrs) as View
} catch (e: Exception) {
return null
}
}
}
由于皮肤包是apk的方式实现,所以需要动态加载皮肤:
class SkinManager {
lateinit var context: Context
var resources: Resources? = null
private constructor()
private object INNER {
val INSTANCE = SkinManager()
}
companion object {
val SKIN_ROOT: String = Environment.getExternalStorageDirectory().absolutePath
@JvmStatic fun getInstance(): SkinManager {
return INNER.INSTANCE
}
}
fun loadSkin(skinName: String) {
try {
val assetManager = AssetManager::class.java.newInstance()
val addAssetPath = AssetManager::class.java.getMethod("addAssetPath", String::class.java)
addAssetPath.invoke(assetManager, SKIN_ROOT + File.separator + skinName + ".apk")
resources = Resources(assetManager, context.resources.displayMetrics, context.resources.configuration)
} catch (e: Exception) {
e.printStackTrace()
}
}
fun getColor(refId: Int): Int {
val entryName = context.resources.getResourceEntryName(refId)
val typeName = context.resources.getResourceTypeName(refId)
val resId = resources?.getIdentifier(entryName, typeName, context.packageName) ?: 0
if (resId != 0) {
return resources!!.getColor(resId)
}
return context.resources.getColor(refId)
}
fun getDrawable(refId: Int): Drawable {
val entryName = context.resources.getResourceEntryName(refId)
val typeName = context.resources.getResourceTypeName(refId)
val resId = resources?.getIdentifier(entryName, typeName, context.packageName) ?: 0
if (resId != 0) {
return resources!!.getDrawable(resId)
}
return context.resources.getDrawable(refId)
}
}
至此Android换肤的效果就实现了。