在 Koltin 中膨胀嵌套布局

发布于 2025-01-15 16:31:04 字数 4821 浏览 5 评论 0原文

我有一个约束布局,其中有 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>

这是其中定义的 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 它工作得很好。我怎样才能访问这些元素?

I have a Constraint layout which 2 main elements inside it: a nested scroll view with a list of elements defined inside another layout file and a textview used as a footer.
This is the 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>

This is the list_item_menu_fragment.xml layout defined in it:

<?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>

This is my fragment:

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
}

If I tryo access to my list of elements using binding.chargeButton it says that i cannot do it (because it is in another layout). If I do binding.menuTitle it works perfectly. How can I access to those elements?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

煮酒 2025-01-22 16:31:04

正如 ViewBinding 中所写

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

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

更改为

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

现在

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

您可以调用 binding.listItemMenu.chargeButton

As written in ViewBinding

An instance of a binding class contains direct references to all views
that have an ID in the corresponding layout.

You have to assign an ID to the included layout and then you can call it from the parent view binding class.

Change from

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

to

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

Now you can call binding.listItemMenu.chargeButton

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文