1

我有一个LinearLayout(horizontal)里面的ScrollView。我正在尝试以编程方式在里面添加TextView's 和's 。ButtonLinearLayout

我的代码没有错误但几乎没有故障。我ScrollView唯一的延伸TextView不是为了Button's。抱歉解释不充分,也许截图有帮助: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>
4

2 回答 2

2

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

于 2020-05-21T02:38:40.037 回答
0

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

LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT)
于 2020-05-21T01:54:12.853 回答