
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/table1">
</TableLayout>
public void click(View v) {
if(row.getText().length()>0&&column.getText().length()>0){
//把输入的行和列转为整形
int row_int=Integer.parseInt(row.getText().toString())
int col_int=Integer.parseInt(column.getText().toString())
//获取控件tableLayout
tableLayout = (TableLayout)findViewById(R.id.table1)
//清除表格所有行
tableLayout.removeAllViews()
//全部列自动填充空白处
tableLayout.setStretchAllColumns(true)
//生成X行,Y列的表格
for(int i=1i<=row_inti++)
{
TableRow tableRow=new TableRow(MainActivity.this)
for(int j=1j<=col_intj++)
{
//tv用于显示
TextView tv=new TextView(MainActivity.this)
//Button bt=new Button(MainActivity.this)
tv.setText("("+i+","+j+")")
tableRow.addView(tv)
}
//新建的TableRow添加到TableLayout
tableLayout.addView(tableRow, new TableLayout.LayoutParams(MP, WC,1))
}
}else{
Toast.makeText(MainActivity.this,"请输入数值",1).show()
}
}
好像有个方法,用listview 在他item中可以动态添加各种控件的,具体看下安卓listview的用法,我也遇到过,不过我是在listview 里面添加8行数据有textview 和EditText欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)