android - 以编程方式添加到 TableLayout 的 TextView 与父级的宽度不匹配
问题描述
我想TableLayout
用两列创建一个。第一列将在内部显示drawable,ImageView
并且应该具有图片的宽度。第二列应占用所有剩余空间(因此匹配行的宽度)。
TableLayout(context).apply {
myList.forEach {
val tableRow = TableRow(context)
tableRow.setBackgroundColor(Color.GREEN)
tableRow.addView(ImageView(context).apply {
setImageResource(R.drawable.ic_blabla)
})
tableRow.addView(TextView(context).apply {
text = it.code.toString()
setBackgroundColor(Color.YELLOW)
layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.WRAP_CONTENT)
})
addView(tableRow)
}
myLinearLayout.addView(this)
}
表格布局以编程方式添加到线性布局中:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/myLinearLayout"/>
结果,我得到了两列,但都有内容的宽度。该行具有绿色背景,因此我可以看到它具有屏幕的宽度,但具有黄色背景的第二列具有内部显示的文本的宽度。
知道如何设置第二列宽度以占用所有空间吗?
解决方案
为列设置layout_weigth="1"
属性,它将为视图分配额外的重要性,因此列将能够在屏幕上填充更多空间。
推荐阅读
- python - 对数据框进行排序后,为什么 loc 和 iloc 有区别?
- python - Pandas Groupby 以递减顺序替换值
- racket - 禁止对使用 eval 的球拍中的特定功能进行类型检查
- python - python 3 如何将项目从另一个列表追加到列表中
- php - DataTables,将数据表导出到列值有逗号的 CSV 文件
- typescript - webpack可以用动态路径静态导入吗
- javascript - 为什么我的 javascript 需要 2 次点击而不是 1 次?
- amazon-web-services - 如何使用 boto3 为 AWS S3 存储桶中的每个 IAM 用户创建单独的 cloudtrail?
- ios - 从 URL 呈现 Files 文档
- reactjs - 如何在antd中将今天日期设置为默认日期?