android 简单实现应用内更换主题色 换肤

18 篇文章 0 订阅
14 篇文章 0 订阅

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>

 

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

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值