在 Koltin 中膨胀嵌套布局

Bab*_*ara 1 xml android android-layout layout-inflater kotlin

我有一个约束布局,其中有 2 个主要元素:一个嵌套滚动视图,其中包含另一个布局文件中定义的元素列表和用作页脚的文本视图。这是 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>
Run Code Online (Sandbox Code Playgroud)

这是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>
Run Code Online (Sandbox Code Playgroud)

这是我的片段:

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
}
Run Code Online (Sandbox Code Playgroud)

如果我尝试使用它访问我的元素列表,binding.chargeButton则表示我无法执行此操作(因为它位于另一个布局中)。如果我binding.menuTitle这样做,效果会很完美。我怎样才能访问这些元素?

Hay*_*ssi 5

正如ViewBinding中所写

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

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

更改自

<include layout="@layout/list_item_menu_fragment" />
Run Code Online (Sandbox Code Playgroud)

<include android:id="@+id/listItemMenu" layout="@layout/list_item_menu_fragment" />
Run Code Online (Sandbox Code Playgroud)

现在你可以打电话binding.listItemMenu.chargeButton