столкнулся с небольшой проблемой. По сути, у меня есть карта (k: строка, v: строка), и я пытаюсь динамически добавить их пары ключ-значение в текстовые представления в макете ограничения. Размер карты может варьироваться, поэтому мне нужно добавлять их динамически. Строки появляются разбитыми друг на друга в android. Хотел узнать, что я мог сделать не так?
Kotlin файл
val params = LayoutParams(
LayoutParams.MATCH_PARENT, // width
LayoutParams.WRAP_CONTENT // height
)
var firstPass = true
var prevResponseId = 0
for ((q, a) in model.questionResponse) {
val question = TextView(context)
val response = TextView(context)
question.gravity = Gravity.CENTER
response.gravity = Gravity.CENTER
question.text = q
response.text = a
question.id = View.generateViewId()
response.id = View.generateViewId()
constraintLayout_book.addView(question, params)
constraintLayout_book.addView(response, params)
val constraintSet = ConstraintSet()
constraintSet.clone(constraintLayout_book)
//Question
if (firstPass) {
constraintSet.connect(R.id.book_word_cloud, BOTTOM, question.id, ConstraintSet.TOP)
constraintSet.connect(question.id, ConstraintSet.TOP, R.id.book_word_cloud, ConstraintSet.BOTTOM)
} else {
val prevResponse: TextView = view.findViewById<TextView>(prevResponseId)
constraintSet.connect(prevResponse.id, ConstraintSet.BOTTOM, question.id, ConstraintSet.TOP)
constraintSet.connect(question.id, ConstraintSet.TOP, prevResponse.id, ConstraintSet.BOTTOM)
}
constraintSet.connect(question.id, ConstraintSet.START, R.id.constraintLayout_book, ConstraintSet.START)
constraintSet.connect(question.id, ConstraintSet.END, R.id.constraintLayout_book, ConstraintSet.END)
constraintSet.connect(question.id, ConstraintSet.BOTTOM, response.id, ConstraintSet.TOP)
//Response
constraintSet.connect(response.id, ConstraintSet.TOP, question.id, ConstraintSet.BOTTOM)
constraintSet.connect(response.id, ConstraintSet.START, R.id.constraintLayout_book, ConstraintSet.START)
constraintSet.connect(response.id, ConstraintSet.END, R.id.constraintLayout_book, ConstraintSet.END)
constraintSet.connect(response.id, ConstraintSet.BOTTOM, R.id.book_guideline_footer, ConstraintSet.TOP)
firstPass = false
prevResponseId = response.id
constraintSet.applyTo(constraintLayout_book)
}
XML файл
<ScrollView
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:id="@+id/scrollingLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout_book"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.Guideline
android:id="@+id/book_guideline_footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95"/>
...
<TextView
android:id="@+id/book_word_cloud"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Word cloud"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/book_image" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
android Изображение