ScrollView 拉伸不够

ScrollView doesn't stretch enough

我在 ScrollView 里面有一个 LinearLayout(horizontal)。我正在尝试以编程方式在 LinearLayout 中添加 TextViewButton

我的代码没有错误,但有小故障。我的 ScrollView 只拉伸 TextView,不拉伸 Button。抱歉解释不充分,也许截图有帮助:Imgur.

❒ Eve gir. 是一个 Button 剩下的是 TextView.

如何让 ScrollView 伸展并包裹我的 Button。我尝试设置 fillViewPort 但它没有用。

TextView 添加代码:

 private fun createText(prTx: String, color: String) {

    val text = TextView(this)
    text.text = prTx
    text.gravity = Gravity.CENTER_HORIZONTAL
    text.textSize = 18F
    text.setShadowLayer(5F,3F,2F,Color.BLACK)

    when (color) {
        "n" -> text.setTextColor(Color.WHITE)
        "p" -> text.setTextColor(Color.rgb(255,182,193))
        "m" -> text.setTextColor(Color.rgb(182,207,255))
        "cm" -> text.setTextColor(Color.rgb(182, 242, 227))
        "olay" -> {
            text.setShadowLayer(5F,3F,2F,Color.rgb(100,0,166))
            text.setTextColor(Color.rgb(75,0,130))
        }
    }

    text.textAlignment = TextView.TEXT_ALIGNMENT_CENTER
    text.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
    val layout = findViewById<LinearLayout>(R.id.llText)
    layout.addView(text)
}

Button 添加代码

private fun createButton(prTx: String, clk: Int) { 
    val button = Button(this)
    button.text = prTx
    button.setBackgroundColor(Color.TRANSPARENT)
    button.gravity = Gravity.CENTER_HORIZONTAL;
    button.textSize = 18F
    button.transformationMethod = null;
    button.setTextColor(Color.WHITE)
    button.setShadowLayer(10F,5F,5F, Color.BLACK)
    button.textAlignment = TextView.TEXT_ALIGNMENT_CENTER

    button.setOnClickListener {
        when (clk) {         
        }
    }

    button.layoutParams = LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.MATCH_PARENT)
    val layout = findViewById<LinearLayout>(R.id.llText)
    layout.addView(button)
}

布局的 XML 代码

    <ScrollView
    android:id="@+id/sv"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:background="#5B34515E"
    android:fadeScrollbars="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="@+id/ivMutfak"
    app:layout_constraintHeight_default="wrap"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="@+id/clMiddle"
    app:layout_constraintTop_toBottomOf="@+id/clMiddle"
    app:layout_constraintVertical_bias="0.0">

    <LinearLayout
        android:id="@+id/llText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:orientation="vertical" />
</ScrollView>

您应该使用 LinearLayout.LayoutParams.WRAP_CONTENT 而不是 ViewGroup.LayoutParams.MATCH_PARENT。试试这个代码

LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT)

问题不是 ScrollView,而是 LinearLayout。当我删除 `LinearLayout 的 android:layout_margin's 时问题解决了。我仍然不知道为什么,但这个解决方案对我有用。