为什么我的合并布局在 LinearLayout 中出现两次?
Why is my merge layout showing up twice inside LinearLayout?
我在父 LinearLayout 中有一个合并布局。出于某种原因,当我 运行 我的应用程序时,合并布局在片段中出现了两次,一个在另一个之上。布局检查器显示合并布局中重复出现在第一组下方的所有元素,并且都直接位于 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
中将父布局充气 2 次。
解决方案
替换为:_contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
有了这个:_contentBinding = ChildBinding.bind(binding.root)
我在父 LinearLayout 中有一个合并布局。出于某种原因,当我 运行 我的应用程序时,合并布局在片段中出现了两次,一个在另一个之上。布局检查器显示合并布局中重复出现在第一组下方的所有元素,并且都直接位于 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
中将父布局充气 2 次。
解决方案
替换为:_contentBinding = ChildBinding.inflate(layoutInflater, binding.root)
有了这个:_contentBinding = ChildBinding.bind(binding.root)