Android TableLayout programmatically

前端 未结 3 2010
没有蜡笔的小新
没有蜡笔的小新 2020-12-10 08:44

I learned how to create UI using XML file.But please help me to know how to do it programmatically without using XML files especially for other than LinearLayout.

3条回答
  •  爱一瞬间的悲伤
    2020-12-10 09:29

    The below code example is given Here.

    public class tablelayout extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    
    //initialize a button and a counter
    Button btn;
    int counter = 0;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        // setup the layout
        setContentView(R.layout.main);
    
        // add a click-listener on the button
        btn = (Button) findViewById(R.id.Button01);
        btn.setOnClickListener(this);        
    
    }
    
    // run when the button is clicked
    public void onClick(View view) {
    
        // get a reference for the TableLayout
        TableLayout table = (TableLayout) findViewById(R.id.TableLayout01);
    
        // create a new TableRow
        TableRow row = new TableRow(this);
    
        // count the counter up by one
        counter++;
    
        // create a new TextView
        TextView t = new TextView(this);
        // set the text to "text xx"
        t.setText("text " + counter);
    
        // create a CheckBox
        CheckBox c = new CheckBox(this);
    
        // add the TextView and the CheckBox to the new TableRow
        row.addView(t);
        row.addView(c);
    
        // add the TableRow to the TableLayout
        table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    
    }
    

    }

提交回复
热议问题