1.引包(不引也可以,使用sharedPreferences)

implementation 'com.tencent:mmkv:1.0.10'

 

2.初始化(如果没有引包,此步省略)

MyApp-onCreate方法中添加
MMKV.initialize(this)
MMKV.defaultMMKV()

 

3.style中配置两个皮肤,并定义attr属性

<resources>
    <attr name="bgColor" format="color|reference" />
    <attr name="textColor" format="color|reference" />
    <!-- Base application theme. -->
    <style name="DefaultTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="bgColor">@color/def_bgColor</item>
        <item name="textColor">@color/def_textColor</item>
    </style>

    <style name="DarkTheme" parent="DefaultTheme">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="bgColor">@color/dark_bgColor</item>
        <item name="textColor">@color/dark_textColor</item>
    </style>

    <style name="ViewStyle">
        <item name="android:background">?attr/bgColor</item>
    </style>

    <style name="TextStyle">
        <item name="android:textColor">?attr/textColor</item>
    </style>
</resources>

 

4.在BaseActivity方法中设置主题

abstract class BaseActivity : AppCompatActivity() {
    protected var mmkv: MMKV? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        val skin = MMKV.defaultMMKV().decodeInt("skin", 1)
        if (skin == 1) {
            setTheme(R.style.DefaultTheme)
        } else if (skin == 2) {
            setTheme(R.style.DarkTheme)
        }
        super.onCreate(savedInstanceState)

        mmkv = MMKV.defaultMMKV()
        setContentView(getLayoutId())
        init()
    }

    protected abstract fun init()

    protected abstract fun getLayoutId(): Int
}

 

5.继承BaseActivity进行换肤

class MainActivity : BaseActivity() {
    override fun init() {
        findViewById<Button>(R.id.t1).setOnClickListener {
            mmkv?.encode("skin", 1)
            recreate() //重启画面
        }
        findViewById<Button>(R.id.t2).setOnClickListener {
            mmkv?.encode("skin", 2)
            recreate() //重启画面
        }
        findViewById<Button>(R.id.t3).setOnClickListener {
            startActivity(Intent(this, TestActivity::class.java))
        }
    }

    override fun getLayoutId() = R.layout.activity_main
}

-activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <View
        android:id="@+id/vvv"
        style="@style/ViewStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <Button
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="t1" />

    <Button
        android:id="@+id/t2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/t1"
        android:text="t2" />

    <Button
        android:id="@+id/t3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toEndOf="@+id/t2"
        android:text="t3" />

    <TextView
        style="@style/TextStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Hello World!" />

</RelativeLayout>

 

至此,更换主题色已完成,这种方法比较简单,应该有进一步优化的空间,此处暂时先搁置,后续用的再写

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐