在Android Studio中使用Data Binding需要进行以下配置:
android {
...
buildFeatures {
dataBinding = true
}
}
dependencies {
...
implementation 'com.android.databinding:library:3.5.0'
}
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding: ActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main)
val user = User("John", "Doe")
binding.user = user
}
}
注意:如果仍然出现'Unresolved reference: databinding”错误,请先检查Gradle版本和Android插件版本,确保它们与依赖项中指定的版本兼容。