在 Koltin 中膨胀嵌套布局
我有一个约束布局,其中有 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 ViewBinding 中所写
您必须为包含的布局分配一个 ID,然后您可以从父视图绑定类中调用它。
更改为
现在
您可以调用
binding.listItemMenu.chargeButton
As written in ViewBinding
You have to assign an ID to the included layout and then you can call it from the parent view binding class.
Change from
to
Now you can call
binding.listItemMenu.chargeButton