android - 在 tableRow Kotlin 中未正确创建按钮
问题描述
我正在尝试动态创建一个带有 Button 的 tableRow 视图。但是创建的 Button 的大小是 0dp 宽度和高度(正如我在 Layout Inspector 中看到的那样)
MainActivity.kt 代码:
val tableRow = TableRow(this)
tableRow.layoutParams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.MATCH_PARENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
val button = Button(this)
button.layoutParams = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
button.text = "Hello"
tableRow.addView(button)
deviceLayout.addView(tableRow)
活动主.xml:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
...
<TableLayout
android:id="@+id/deviceLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
app:layout_constraintTop_toBottomOf="@+id/StatusString"
tools:layout_editor_absoluteX="0dp" >
</TableLayout>
</androidx.constraintlayout.widget.ConstraintLayout>```
解决方案
推荐阅读
- python - 向使用 Python 的每个人授予对 Vertica SQL 表的访问权限
- r - 如何计算特定位置多年来每种化学品的加权移动平均浓度?
- python - 如何正确地将一列复制到另一列中(没有复制错误)?
- macports - 升级 macports 时如何解决请求的变体错误
- android - 如何从“onDataChange”侦听器内部将字符串添加到 ArrayList?
- docker - 如何在 Kubernetes 中将用于容器的 Docker 映像的摘要作为环境变量传递
- r - R 中的 Twitter API - Geo 的关注者数量
- php - 如何在foreach中获取当前数组键
- node.js - 如何在没有 createdAt 字段的情况下按年龄升序对 mongodb 查询结果进行排序?(新到旧)
- android - BasicNetwork.performRequest:Volley 的意外响应代码 422