在Koltin中充气嵌套布局



我有一个Constraint布局,里面有两个主要元素:一个嵌套的滚动视图,在另一个布局文件中定义了元素列表,另一个文本视图用作页脚。这是xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.core.widget.NestedScrollView
android:id="@+id/menu_scrollview"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:context=".sections.main.menu.MenuFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/menu_title"
style="@style/AppTheme.TextTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/mid_small_margin"
android:text="@string/menu_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<include layout="@layout/list_item_menu_fragment" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>

<TextView
android:id="@+id/menu_title_2"
style="@style/AppTheme.TextTitle1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/mid_small_margin"
android:text="@string/menu_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是其中定义的list_item_menu_fragment.xml布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/menu_elements_list"
style="@style/RectangularTextViewMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="@dimen/mid_small_margin"
android:layout_marginTop="20dp"
android:layout_marginBottom="32dp"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="@+id/menu_item_profile"
app:layout_constraintBottom_toBottomOf="@+id/links_title"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/charge_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fontFamily="@font/montserrat_bold"
android:gravity="center_vertical"
android:textColor="@color/black"
android:text="@string/menu_pay_charge"
app:drawableStartCompat="@drawable/ic_menu_charge"
app:drawableEndCompat="@drawable/ic_arrow_right" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/pay_online_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:fontFamily="@font/montserrat_bold"
android:gravity="center_vertical"
android:textColor="@color/black"
android:text="@string/menu_pay_online"
app:drawableStartCompat="@drawable/ic_menu_pay_online"
app:drawableEndCompat="@drawable/ic_arrow_right" />
</LinearLayout>

这是我的片段:

class MenuFragment : Fragment() {
private var _binding: FragmentMenuBinding? = null
private val menuViewModel by viewModels<MenuViewModel>()
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentMenuBinding.inflate(inflater, container, false)
return binding.root
}

如果我尝试使用binding.chargeButton访问我的元素列表,它会说我不能这样做(因为它在另一个布局中(。如果我做binding.menuTitle,它会完美工作。我如何访问这些元素?

如ViewBinding 中所写

绑定类的实例包含对所有视图的直接引用其在相应布局中具有ID。

您必须为包含的布局分配一个ID,然后才能从父视图绑定类中调用它。

的变化

<include layout="@layout/list_item_menu_fragment" />

<include android:id="@+id/listItemMenu" layout="@layout/list_item_menu_fragment" />

现在您可以调用binding.listItemMenu.chargeButton

最新更新