Reference content: https://blog.csdn.net/weixin_39251617/article/details/79711668
In the demand coming this time, there is a cell merge content, and a certain column of the dynamically generated table should be classified and merged.
A a1 a11
A a2 a21
B b1 b11
For example, for this kind of data, A will be merged into one cell.
I thought it shouldn't be that difficult, it's like a calculator with html!
Baidu Android Table, a bunch of tablelayout content comes out. After watching it for a while, I felt something was wrong, and then I realized that GridLayout should be used.
Properties of GridLayout:
android:columnCount="3" This is fixed, the data has three columns
The properties of the TextView inside:
android:layout_columnWeight="1" This controls the average width of each grid
android:layout_rowSpan="2"
android:layout_rowWeight="1" These two are used together, which means that one grid occupies two rows
In this way, I can basically pick up a calculator.
But to be dynamically generated, it must be written in java code. . . .
textView.set. . . There is nothing inside!
I have the idea to add these parameters to style.xml and use textView.setTextAppearance to bind these styles, but it fails! (I don’t know why it failed)
Finally found the strategy. You must first new GridLayout.LayoutParams();
set rowSpec and columnSpec in this param.
The final solution (pseudo code)
// 增加一个TextView到GridLayout
add2Table(内容,占行数){
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.rowSpec =GridLayout.spec(GridLayout.UNDEFINED, 占行数, 1f);
params.columnSpec =GridLayout.spec(GridLayout.UNDEFINED,1f);
TextView textView = new TextView(ctx);
textView.setText(内容);
gridLayout.addView(textView,params);
}
// 对数据循环加入(BigList 是ABCD之类的大类,每个元素中包含abcd)
BigList.forEach(A->
smallList = A.getSmallList()
add2table(A.getText(),smallList.size);// A占行数跟包含的abcd数量有关
smallList.forEach(a->
add2table(a.getText(),1);// a占一行
)
)
)