动态复选框创建

我想在我的Android应用程序运行时动态创建一组复选框。 当应用程序运行时,除了按钮之外没有任何显示。 我忘记了什么? 提前致谢!

public class DataNotificationSurvey extends Activity { private Date timeStamp; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.datanotifylayout); final Button notifySubmitButton = (Button) findViewById(R.id.notifySubmitButton); TableLayout t1 = (TableLayout)findViewById(R.id.notificationTableLayout); for(int i = 0; i < 5; i++) { TableRow tr = new TableRow(this); CheckBox chk = new CheckBox(this); chk.setText(Integer.toString(i)); tr.addView(chk); t1.addView(tr); } notifySubmitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } 

 //My xml layout, will be changing this later to the one posted below to see if it works.       

这对我来说很好。

 public class DynamicTableRowWithCheckBox extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final Button notifySubmitButton = (Button) findViewById(R.id.myButton); TableLayout table = (TableLayout) findViewById(R.id.myTable); for (int i = 0; i < 3; i++) { TableRow row = new TableRow(this); CheckBox chk = new CheckBox(this); chk.setText(Integer.toString(i)); row.addView(chk); table.addView(row); } notifySubmitButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } } 
       

将您的XML更改为:

          

这会将TableLayout的layout_height移除到wrap_content,并删除TableRow的高度和宽度设置,因为“TableRow的子节点不需要在XML文件中指定layout_width和layout_height属性.TableRow始终强制执行这些值分别是MATCH_PARENT和WRAP_CONTENT。“