我想用这样的行来创建自定义TableLayout:
电视用于TextView,即我想在行中添加11个TextView:
每行以标题开头,然后我添加5对TextView,这样表格行就像屏幕一样宽.
这是我的代码:
public class FlowTable extends TableLayout {
private Context context;
public FlowTable(Context context) {
super(context);
this.context = context;
}
public FlowTable(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public void addContent(List data) {
TableRow tableRow = new TableRow(context);
LayoutParams params = new LayoutParams(TableRow.LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1);
for (int i = 0; i < data.size(); i++) {
if (i % 5 == 0) {
this.addView(tableRow, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tableRow = new TableRow(context);
TextView tvRange = new TextView(context);
tvRange.setLayoutParams(params);
tvRange.setText(genRange(i+1));
tableRow.addView(tvRange);
}
TextView tvDistance = new TextView(context);
tvDistance.setLayoutParams(params);
tvDistance.setText(String.valueOf(data.get(i).distance));
TextView tvResult = new TextView(context);
tvResult.setLayoutParams(params);
tvResult.setText(data.get(i).result);
tableRow.addView(tvDistance);
tableRow.addView(tvResult);
}
}
private String genRange(int currIndex){
/********************/
return somestring;
}
}
使用表格:
android:layout_width="match_parent"
android:layout_height="match_parent" >
android:id="@+id/flowTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
在片段中:
View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
flowTable.addContent(data);
问题是:屏幕只是空的!什么都没有.在我将布局参数添加到textview之前,它可以工作,但是行没有占用屏幕宽度.我的初始解决方案基于LinearLayout示例,因为TableRow是LinearLayout的扩展.但我不能让它发挥作用.
谢谢.