2024年鸿蒙最全Databinding+LiveData轻松实现无重启换肤_ios databing 换肤(1),腾讯面试官工资

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!


img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

需要这份系统化的资料的朋友,可以戳这里获取

data class Theme(
    val content: Int,
    val background: Int,
)

object Themes {
    val Default = Theme(Color.RED, Color.GRAY)
    val Day = Theme(Color.BLACK, Color.WHITE)
    val Night = Theme(Color.MAGENTA, Color.BLACK)
}

object AppTheme {
    val background = MutableLiveData<ColorStateList>()
    val content = MutableLiveData<ColorStateList>()

    init {
        update(Themes.Default)
    }

    fun update(theme: Theme) {
        background.value = ColorStateList.valueOf(theme.background)
        content.value = ColorStateList.valueOf(theme.content)
    }
}   

在布局文件中使用皮肤

直接引入AppTheme单例,livedata会关联生命周期,不用担心资源释放

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>

        <import type="ezy.demo.theme.AppTheme" />
    </data>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:background="#EEEEEE">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            app:background="@{AppTheme.INSTANCE.background}"
            android:gravity="center"
            android:orientation="vertical">

            <SeekBar
                android:layout_width="300dp"
                android:layout_height="wrap_content"
                android:max="100"
                android:progress="50"
                android:progressBackgroundTint="@{AppTheme.INSTANCE.background}"
                android:progressTint="@{AppTheme.INSTANCE.content}"
                android:thumb="@android:drawable/ic_btn_speak_now"
                android:thumbTint="@{AppTheme.INSTANCE.content}" />

            <TextView
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:layout_margin="10dp"
                android:gravity="center"
                android:text="Hello World!"
                android:textColor="@{AppTheme.INSTANCE.content}"
                app:drawableTint="@{AppTheme.INSTANCE.content}"
                app:drawableTopCompat="@android:drawable/ic_media_pause" />

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/btn_default"
                    android:layout_width="100dp"
                    android:layout_height="40dp"
                    android:gravity="center"
                    android:text="Default"
                    android:textColor="@{AppTheme.INSTANCE.content}" />

                <TextView
                    android:id="@+id/btn_day"
                    android:layout_width="100dp"
                    android:layout_height="40dp"
                    android:gravity="center"
                    android:text="Day"
                    android:textColor="@{AppTheme.INSTANCE.content}" />

                <TextView
                    android:id="@+id/btn_night"
                    android:layout_width="100dp"
                    android:layout_height="40dp"
                    android:gravity="center"
                    android:text="Night"
                    android:textColor="@{AppTheme.INSTANCE.content}" />
            </LinearLayout>
        </LinearLayout>

    </FrameLayout>

</layout>

关联生命周期

实际上 Databinding+ObserverableField也能实现无重启换肤,但ObserverableField不能关联生命周期,资源释放麻烦一些


class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main)

        // 关联生命周期
        binding.lifecycleOwner = this



![img](https://img-blog.csdnimg.cn/img_convert/5359d8cd299724f76a5d4cc01b6a79e7.png)
![img](https://img-blog.csdnimg.cn/img_convert/5df96677568171c52cf947257fd0cb1d.png)

**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化的资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

朋友,可以戳这里获取](https://bbs.csdn.net/topics/618636735)**


**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 10
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值