android - binding.root 表示什么?
问题描述
以下是我的代码片段。
class DashBoardHolder(val binding: ActivityDashBoardBinding) : RecyclerView.ViewHolder(binding.root) {
internal var tvName: TextView = itemView.findViewById(R.id.textViewGrandrukName)
解决方案
binding.root 是对根视图的引用。
根视图是 布局中最外层的视图容器。
当您调用 binding.root 时,将返回 LinearLayout 根视图。(以下 xml 代码)
在视图绑定之前,当我们调用 findViewById() 时,我们可以从任何布局调用任何 id,即使给定布局除外。它不会显示编译时错误,就像你从其他布局调用 id 一样,该视图不在您提供的布局中.当我们运行时,我们将获得“Null”对象引用。但是视图绑定将从根视图提供所有绑定ID。我们只能调用biding.name和biding.button。我们不能调用其他布局绑定id。所以当我们使用时查看投标,我们不会得到“NULL”。这是视图绑定的特定功能。都是因为根视图。
<LinearLayout ... > <TextView android:id="@+id/name" /> <ImageView android:cropToPadding="true" /> <Button android:id="@+id/button" android:background="@drawable/rounded_button" /> </LinearLayout>
推荐阅读
- microsoft-graph-api - 从 Microsoft Graph API 检索所有用户详细信息
- r - 使用 `shinycssloaders::withSpinner` 覆盖多个输入
- amazon-web-services - 在同一个无服务器框架中创建存储桶和 lambda 触发器
- python - 如何访问 Sympy 中的所有矩阵元素并将它们与另一个矩阵进行比较(即大于或小于)?
- c# - 使用 DocumentFormat.OpenXml C# 修改 excel 自定义变量
- azure - 如何使用 MSI 从另一个租户中的服务访问资源?
- android - Android Studio xml预览窗口没有出现预览
- google-cloud-platform - 即使从容器注册表中删除了镜像,GCP VM 也会运行容器镜像
- java - HIVE安装错误| '不是一个有效的标识符'
- pandas - 通过对 pandas 中的多个列进行 AND 运算来构建决策列