在我的 fragment 布局中,我添加了一个包含包含的布局
<?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>
我访问 fragment 中的 View send_btn_view.setOnClickListener {
Log.e("button","clicked")
}
合成进口是import kotlinx.android.synthetic.main.item_user_input.*
当我运行应用程序时,我得到了错误Unresolved reference: item_user_input
最佳答案
你不能,因为如果同一个布局被多次包含,合成库将不知道该怎么做。
无论如何,不推荐使用 Kotlin Synthetic,Google 官方建议迁移到 View /数据绑定(bind):
https://developer.android.com/topic/libraries/view-binding/migration
由于您已经使用数据绑定(bind),我建议使用绑定(bind)类来访问 View 。这样您就可以明确提及包含的布局。
首先,在包含的布局中添加一个 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" />
二、访问 View :binding.itemUserInput.sendBtnView
关于android - 如何使用 android 中的 kotlin 合成从包含的布局中访问 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66999691/