0

ちょっとしたトラブルに。基本的に私は Map(k:string, v:string) を持っていて、キーと値のペアを制約レイアウトのテキスト ビューに動的に追加しようとしています。マップのサイズはさまざまなので、動的に追加する必要があります。文字列は、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>

アンドロイド画像

ここに画像の説明を入力

4

0 に答える 0