我想创建带有这样的行的自定义表格布局:
TV 用于 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<ResultItem> 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;
}
}
使用表:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<packagename.FlowTable
android:id="@+id/flowTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
在片段中:
View root = inflater.inflate(R.layout.fragment_session_summary, container, false);
FlowTable flowTable = (FlowTable)root.findViewById(R.id.flowTable);
flowTable.addContent(data);
问题:屏幕只是空的!什么都没有。在我将布局参数添加到 textview 之前,它可以工作,但是 row 没有占据屏幕宽度。我最初的解决方案基于 LinearLayout 示例,因为 TableRow 是 LinearLayout 的扩展。但我无法让它发挥作用。
谢谢。