ScrollView拉伸不足

问题描述 投票:0回答:2

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

我的代码工作正常,没有错误,但故障很少。我的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>

我在ScrollView中有一个LinearLayout(水平)。我正在尝试以编程方式在LinearLayout内添加TextView和Button。我的代码工作正常,没有错误,但故障很少。我的...

android kotlin android-scrollview
2个回答
0
投票

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


0
投票

问题不是ScrollView,是LinearLayout。删除android:layout_margin...LinearLayout后,问题解决了。我仍然不知道为什么,但是这种解决方案对我有用。

© www.soinside.com 2019 - 2024. All rights reserved.