动态添加tableview

        TableLayout table=(TableLayout)findViewById(R.id.shoppingCartTable);  

        table.setBackgroundColor(Color.BLACK);
        TableLayout.LayoutParams params=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,
        		TableLayout.LayoutParams.WRAP_CONTENT); 
        params.leftMargin =1;
        params.rightMargin =1;
        params.topMargin =1;
        params.bottomMargin =1;
        TableRow.LayoutParams params2 =  new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
        		TableRow.LayoutParams.WRAP_CONTENT); 
        params2.leftMargin =2;
        params2.rightMargin =2;
        params2.topMargin =2;
        params2.bottomMargin =2;
        
        for(int i=0;i<20;i++){//20  
            TableRow row=new TableRow(this);  
            row.setLayoutParams(params);  
            row.setBackgroundColor(Color.BLUE); 
            row.setPadding(1, 1, 1, 1);
            
            for(int j=0;j<1;j++){//1  
                TextView col=new TextView(this);  
                col.setLayoutParams(params2);
                col.setText("i="+i);  
                col.setTextColor(0xFFFF0000); 

                row.addView(col);//  
            }  
            table.addView(row);//  
        } 


这一段动态添加tableview的代码,保留下来以后说不定用的到

你可能感兴趣的:(tableview,添加,动态)