目录
Steps to use data binding to replace calls to findViewById()
Steps for binding views to data
Steps to use data binding to replace calls to findViewById()
1. Enable data binding in the android{} of the build.gradle (Module: app) file
dataBinding {
enabled = true
}
2. Use <layout> as the root view in XML layout
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- 各种Layout放在中间 -->
</layout>
3. Define a binding variable (maybe before the onCreate())
private lateinit var binding: ActivityMainBinding
4. Create a binding object, replacing setContentView (obviously