android - 如何在自定义视图上使用视图绑定
问题描述
View Binding 作为 Android Jetpack 的一部分发布
文档:https ://developer.android.com/topic/libraries/view-binding
我的问题是,如何将视图绑定与自定义视图一起使用。Google 文档只有展示的 Activity 和 Fragment。
我试过这个,但什么也没显示。
LayoutInflater inflater = LayoutInflater.from(getContext());
然后,我使用了这个,但又一次,没有运气。
LayoutInflater inflater = (LayoutInflater)
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
我想也许我没有针对我的观点选择正确的布局充气器,但不确定。
解决方案
只需通知根,以及是否要附加到它
init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this)
}
或者
init { // inflate binding and add as view
binding = ResultProfileBinding.inflate(LayoutInflater.from(context), this, true)
}
使用哪种膨胀方法取决于 xml 中的根布局类型。
推荐阅读
- python - 如何忽略beautifulsoup4中的子元素
- heroku - 尽管使用了锁,但 Rufus-scheduler 调度了两次
- javascript - Angular AuthListener - 如何在页面刷新后保持用户登录
- javascript - AJAX分页如何处理在后台更新的新数据(数据库)
- c# - 使用 IF 语句来处理与 XML 相关的事情
- django - 如何让 post_detail 页面正常工作?
- tensorflow - 为最新的 Python 3 版本安装 Tensorflow - Python 3.6.0(跟进)
- python-3.x - 将较少发生的替换为其他
- c# - 在哪里可以找到所有统一智能感知功能等?
- r - 如何从 power BI 中的 r 脚本中运行 r 脚本?