一、启用viewBinding
在你的app的build.gradle的android{}标签内添加
// 启用viewBinding
buildFeatures {
viewBinding true
}
// 解决viewBinding会报红的问题
sourceSets {
debug {
java.srcDirs = ['src/main/java', 'build/generated/data_binding_base_class_source_out/debug/out']
}
}
二、在Activity中使用
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
}
}