我必须在我的应用程序中动态创建复选框。复选框的数量取决于服务器的响应。我可以使用列表视图根据服务器的响应动态创建复选框。但要求是应该加载复选框两列。请检查所附图片。请帮助我。在此先感谢
问问题
3364 次
3 回答
1
动态创建一个 TableLayout 并用复选框填充它。
像这样的东西:
TableLayout tl = new TableLayout(getActivity());
int offset_in_column=0, table_size=/*the size of your answer from the server*/;
TableRow tr=null;
for (int offset_in_table=0; offset_in_table < table_size; offset_in_table++) {
/* maybe you want to do something special with the data from the server here ? */
if (offset_in_column == 0) {
tr = new TableRow(getActivity());
tr.setLayoutParams(new TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
CheckBox check = new CheckBox(getActivity());
check.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* add your code here */
}
});
check.setLayoutParams(new TableRow.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1));
tr.addView(check);
offset_in_column++;
if (offset_in_column == 2) {
tl.addView(tr);
offset_in_column = 0;
}
}
if (offset_in_column != 0)
tl.addView(tr);
于 2012-11-07T05:24:08.707 回答
0
于 2012-11-07T06:56:39.490 回答
0
于 2012-11-07T05:22:05.277 回答