在我的片段布局中,我添加了一个包含 include 的布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="0dp"
android:layout_height="0dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="3"
tools:listitem="@layout/list_item_eceived" />
<include
layout="@layout/item_user_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
我包含的布局
<?xml version="1.0" encoding="utf-8"?>
<layout 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">
<data>
<variable
name="hint"
type="String" />
<variable
name="available"
type="Boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/message_box_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:elevation="2dp"
android:minHeight="50dp"
app:layout_goneMarginStart="6dp">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/message_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="4dp"
android:focusable="true"
android:hint="@{hint}"
android:maxHeight="128dp"
android:padding="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/send_btn_view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/send_btn_view"
android:layout_width="44dp"
android:layout_height="44dp"
android:layout_marginEnd="2dp"
android:paddingStart="12dp"
android:paddingTop="8dp"
android:paddingEnd="8dp"
android:paddingBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
我访问片段中的视图
send_btn_view.setOnClickListener {
Log.e("button","clicked")
}
合成进口为import kotlinx.android.synthetic.main.item_user_input.*
当我运行该应用程序时,我收到错误
Unresolved reference: item_user_input
你不能,因为如果多次包含相同的布局,综合库将不知道该怎么做。
无论如何,Kotlin Synthetic 已被弃用,Google 官方建议迁移到视图/数据绑定:https://developer.android.com/topic/libraries/view-binding/migration https://developer.android.com/topic/libraries/view-binding/migration
由于您已经使用数据绑定,我建议使用绑定类来访问视图。通过这种方式,您可以明确提及包含的布局。
首先,将 id 添加到包含的布局中:
<include
android:id="@+id/item_user_input"
layout="@layout/item_user_input"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
二、访问视图:
binding.itemUserInput.sendBtnView
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)