android - 为什么我的合并布局在 LinearLayout 中出现两次?
问题描述
我在父 LinearLayout 中有一个合并布局。出于某种原因,当我运行我的应用程序时,合并布局在片段中显示了两次,一个在另一个之上。Layout Inspector 显示合并布局中在第一组下方重复的所有元素,并且都直接在 LinearLayout 下方。更奇怪的是,重复的元素具有相同的 id。
父布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/child_layout" />
</LinearLayout>
子布局:
<merge
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="stuff" />
<Button
android:id="@+id/positiveButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/negativeButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
片段代码:
private var _binding: ParentBinding? = null
private val binding get() = _binding!!
private var _contentBinding: ChildBinding? = null
private val contentBinding get() = _contentBinding!!
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = ParentBinding.inflate(layoutInflater)
_contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
return binding.root
}
解决方案
发生了什么?
您在onCreateView
.
解决方案
替换这个:_contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
有了这个:_contentBinding = ChildBinding.bind(binding.root)
推荐阅读
- arrays - Excel VBA:为什么我无法在一个循环中嵌套两个工作表函数?适用于 Excel 但不适用于 VBA
- kubernetes - Istio 将唯一的 URL 路由到特定的 pod
- python - 从其他列中的特定值创建熊猫数据框列
- pixel - 查找多个图像没有值的像素数
- javascript - 比较两个数组中的元素javascript
- php - Squarespace RSS 提要是否被 PHP 文件拉取请求阻止?
- typescript - 在 TypeScript 中使用构造函数列表作为泛型函数的输入时,如何获得正确的类型?
- css - Web 组件 - 调整大小
- python - 动态发现其中的 python 模块和类
- javascript - 带有 Chrome 扩展的 Twitter 输入字段中的字符串注入