【发布时间】:2011-12-18 19:25:59
【问题描述】:
我想用这样的行创建自定义 TableLayout:
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 之前,它可以工作,但行没有占用屏幕宽度。我最初的解决方案是基于 LinearLayout 示例,因为 TableRow 是 LinearLayout 的扩展。但我不能让它工作。 谢谢。
【问题讨论】:
-
我不确定它是否会起作用,但请尝试将
android:stretchColumns="*"添加到您的<FlowTable>标记中。否则,我也会尝试使用另一个addView重载:tableRow.addView(tvRange, params)