更多信息请访问:与华为共建的Harmonyos技术社区https://harmonyos.51cto.com/#zzharmony-view-binding最新版本:Gitee仓库视图是什么?-view-bindingforharmony-鸿蒙应用开发view-binding插件,剔除findComponentById模板代码-没有注解,如何使用编译时生成的Binding类文件?1.在项目根目录的build.gradle文件中引入视图绑定maven仓库地址和classpathbuildscript{repositories{maven{url'https://mirrors.huaweicloud.com/repository/maven/'}maven{url'https://developer.huawei.com/repo/'}jcenter()maven{url'https://dl.bintray.com/eholee/maven'}}dependencies{classpath'com.huawei.ohos:hap:2.4.0.1'//view-bindingclasspath'com.eholee.plugin:view-binding:1.0.1'}}2.引入view-binding插件applyplugin:'com.huawei.ohos.hap'applyplugin:'com.eholee.plugin.view-binding'ohos{...}viewBinding{enabletrue}在featuremoduledependencies的build.gradle文件中{...}3。执行gradlesync自动生成ViewBinding类。生成的目录在特性中的build/generated/source/viewBinding中。类的命名方式沿用获取xml布局文件名CamelCase后的大驼峰法(UpperCase)并附加Binding后缀,如:MainAbilityBinding4。在需要填充layout的地方主要使用了两个API:1.binding=AbilityMainBinding.parse(t他的);2。setClickedListener(newComponent.ClickedListener(){@OverridepublicvoidonClick(Componentcomponent){newToastDialog(MainAbilitySlice.this).setText("click").show();}});}@OverridepublicvoidonActive(){super.onActive();}@OverridepublicvoidonForeground(Intentintent){super.onForeground(intent);}}可选1.提供设置根布局apiparse(Contextcontext,ComponentContainerparent,booleanattachToRoot)2.支持打开和关闭功能模块视图绑定功能:内置功能。在gradle中设置viewBinding{enablefalse//false为off,插件将不会解析该feature下的所有xml布局文件,//true为on,插件将解析该feature下的所有xml布局文件}3.支持单个xml布局文件默认启用和禁用视图绑定功能。如果要禁用它,需要在xml根节点中添加如下信息:xmlns:eholee="http://schemas.eholee.com/viewbinding"eholee:view_binding="false"示例:
