我想箱子定製TableLayout以行是這樣的:定製TableLayout與行多TextViews
電視是TextView的,即我要11個TextViews添加到該行:
每行以標題開頭,然後添加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);
問題:屏幕只是EMP TY!一點都沒有。在我將layout params添加到textview之前它工作正常,但行沒有佔用屏幕寬度。我的初始解決方案基於LinearLayout示例,因爲TableRow是LinearLayout的擴展。但我無法讓它工作。 謝謝。
我不確定它是否可以正常工作,但是嘗試在您的''標記中添加'android:stretchColumns =「*」'。否則,我也會嘗試使用另一個'addView'重載:'tableRow.addView(tvRange,params)' –
Dalmas
2011-12-18 19:34:09