我在LinearLayout(horizontal)
中有一个ScrollView
。我正在尝试以编程方式在TextView
中添加Button
和LinearLayout
。
我的代码工作正常,没有错误,但故障很少。我的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)
}
<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。我的代码工作正常,没有错误,但故障很少。我的...
您应该使用LinearLayout.LayoutParams.WRAP_CONTENT
而不是ViewGroup.LayoutParams.MATCH_PARENT
。试试这个代码
问题不是ScrollView
,是LinearLayout
。删除android:layout_margin...
的LinearLayout
后,问题解决了。我仍然不知道为什么,但是这种解决方案对我有用。