如何设置表格布局的表格列权重
我有两种表格布局。一个用于显示标题,另一个用于显示数据。包含数据的表应该是可滚动的,因此以单独的表布局编写。 xml 如下所示:
<TableLayout
android:id="@+id/title_table_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/upper_line"
android:layout_marginEnd="@dimen/dp108"
android:layout_marginStart="@dimen/dp60"
android:layout_marginTop="@dimen/dp15" />
<ScrollView
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp108"
android:layout_marginStart="@dimen/dp60"
android:layout_marginTop="@dimen/dp15"
app:layout_constraintTop_toBottomOf="@+id/lower_line"
android:scrollbars="vertical"
android:scrollbarSize="@dimen/dp4"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TableLayout
android:id="@+id/data_table_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/upper_line"
/>
列数是固定的,标题也是固定的。但数据是动态的并且可能具有不同的大小。所以我添加了每行和每列的权重。代码如下所示,
fun addRowHeaders(){
val tr = TableRow(context)
tr.layoutParams = getLayoutParams()
tr.weightSum = 7F
tr.addView(getTextView(context?.resources?.getString(R.string.settings_type)!!,0))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_name)!!,1))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_date)!!,2))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_price)!!,3))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_payment_method)!!,4))
binding.titleTableView.addView(tr,getTblLayoutParams())
}
private fun getTextView(title:String, id:Int): View? {
val tv = TextView(context)
tv.text = title
tv.setTextColor(resources.getColor(R.color.update_service_text_color))
tv.textSize = resources.getDimension(R.dimen.table_text)
tv.layoutParams = getLayoutParams(id)
return tv
}
private fun getLayoutParams(id: Int): LayoutParams {
val params = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
if (id == 1 || id == 4)
params.weight = 2F
else
params.weight = 1F
return params
}
我希望第二列和最后一列具有不同的大小。有了标题,它工作得很好。但是,当我对动态数据表使用相同的代码时,它的行为方式不同,它采用最长文本的大小。动态表的代码如下:
fun addTransactionData(){
for (data in transactionList!!){
val tr = TableRow(context)
tr.layoutParams = getLayoutParams()
tr.weightSum = 7F
tr.addView(getTextView(data.type, 0))
tr.addView(getTextView(data.title, 1))
val date = convertDateString(data.date)
tr.addView(getTextView(date, 2))
tr.addView(getTextView(data.price, 3))
tr.addView(getTextView(data.paymentMethod, 4))
binding.dataTableView.addView(tr, getTblLayoutParams())
}
}
请让我知道如何使两个表统一
I have two table layouts. One to display title and one more to display the data. The table with data should be scrollable hence written in a separate table layout.
The xml looks like this:
<TableLayout
android:id="@+id/title_table_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/upper_line"
android:layout_marginEnd="@dimen/dp108"
android:layout_marginStart="@dimen/dp60"
android:layout_marginTop="@dimen/dp15" />
<ScrollView
android:id="@+id/scroll_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/dp108"
android:layout_marginStart="@dimen/dp60"
android:layout_marginTop="@dimen/dp15"
app:layout_constraintTop_toBottomOf="@+id/lower_line"
android:scrollbars="vertical"
android:scrollbarSize="@dimen/dp4"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TableLayout
android:id="@+id/data_table_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/upper_line"
/>
The number of columns are fixed and the titles also are fixed. But the data is dynamic and could be of varied sizes. So I have added the weight for each row and for each column. The code looks like this
fun addRowHeaders(){
val tr = TableRow(context)
tr.layoutParams = getLayoutParams()
tr.weightSum = 7F
tr.addView(getTextView(context?.resources?.getString(R.string.settings_type)!!,0))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_name)!!,1))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_date)!!,2))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_price)!!,3))
tr.addView(getTextView(context?.resources?.getString(R.string.settings_payment_method)!!,4))
binding.titleTableView.addView(tr,getTblLayoutParams())
}
private fun getTextView(title:String, id:Int): View? {
val tv = TextView(context)
tv.text = title
tv.setTextColor(resources.getColor(R.color.update_service_text_color))
tv.textSize = resources.getDimension(R.dimen.table_text)
tv.layoutParams = getLayoutParams(id)
return tv
}
private fun getLayoutParams(id: Int): LayoutParams {
val params = LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
if (id == 1 || id == 4)
params.weight = 2F
else
params.weight = 1F
return params
}
I want the second and last column to be in the different sizes. With the title it is working fine. But when I am using the same code for dynamic data table it does not behave the same way, It takes the size of the longest text. The code for dynamic table is below:
fun addTransactionData(){
for (data in transactionList!!){
val tr = TableRow(context)
tr.layoutParams = getLayoutParams()
tr.weightSum = 7F
tr.addView(getTextView(data.type, 0))
tr.addView(getTextView(data.title, 1))
val date = convertDateString(data.date)
tr.addView(getTextView(date, 2))
tr.addView(getTextView(data.price, 3))
tr.addView(getTextView(data.paymentMethod, 4))
binding.dataTableView.addView(tr, getTblLayoutParams())
}
}
Please let me know how to make this uniform for both the table
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您无法直接控制 TableLayout 中任何列的宽度,只能间接控制布局如何增大或缩小列。
来自文档
此外,列宽匹配是根据 TableLayout 完成的,因此不能保证您的
title_table_view
与您的data_table_view
匹配(如果它们具有相似的值,它们可能看起来像是对齐的)单元格中的长度文本)在
TableRow
中完全忽略weightSum
值,因此
TableLayout
不适合您的需要。在尝试设计具有固定标题列的表格布局时,我遇到了类似的问题,最后我不得不设计自己的
TableLayout
替代品,具有类似的自动基本列大小调整功能,然后您可以直接控制尺寸。您也许可以用它来实现您想要的功能(请参阅 https://github.com 的“额外功能” /Zardozz/FixedHeaderTableLayout )You cannot directly control the width of any column in a TableLayout, you can only indirectly control how the layout grows or shrinks a column.
From the documentation
Also the column width matching is done per TableLayout so there is no guarantee that your
title_table_view
will match yourdata_table_view
(they might look like they align if they have similar length text in the cells)The
weightSum
value is totally ignored in aTableRow
Thus
TableLayout
is not suitable for what you need.I faced similar problems when trying to design a Table Layout with a fixed header column, in the end I had to design my own replacement for
TableLayout
with similar auto basic sizing of columns that you then have director control of the size. You might be able to implement what you want with that (See the "Bonus Feature" of https://github.com/Zardozz/FixedHeaderTableLayout )