踩坑,在Activity中如何使用多个ViewBinding
WeatherActivity.kt
之前在Activity中引用多个布局,傻傻地这么写,其实归根结底是在布局中使用了include
那么应该怎么写呢?
解决方案
直接上最简单的方法
- 在布局中引入id
- 删除冗余代码
- 然后如是用
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
...
weatherBinding= ActivityWeatherBinding.inflate(layoutInflater)
setContentView(weatherBinding.root)
...
}
//用法
weatherBinding.includeNow.placeName.text=viewModel.placeName