viewBinding
- 配置
android{
buildFeatures{
//dataBinding = true
// for view binding :
viewBinding = true
}
}
- 资源名的变化
布局文件的名称为
result_profile.xml
: 使用名为 ResultProfileBinding
3 使用
private lateinit var binding: ResultProfileBinding //布局文件名
override fun onCreate(savedInstanceState: Bundle) {
super.onCreate(savedInstanceState)
binding = ResultProfileBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
}
binding.nametextview.text = "nihaome" //id为 nametextview 的 view
binding.gobutton.setOnClickListener { viewModel.userClicked() }