preface

In our previous examples, we created a form view directly using the designer, and it is common to use code to dynamically add controls to the view during development.

The general steps are:

1. Generate a control, such as a New Button. 2. Set various properties of the control, such as setting the display text of a Button, binding events and so on. 3. Append the control to a container control as a child of the container control. For example: the AddView (…). .4. In the appended parent control, you can specify the layout.

After the dynamic addition is complete, we also need to be able to get the values for the dynamically added controls. Let’s say I add a text field, and I want to get the value that the user put in that text field. Android view control, there is a property Tag, we can easily use this property to store our special Tag, with this special Tag to mark our control.

Steps to get a control:

1. Get container controls

2. Iterate through all the child controls contained in the container control, and find the control we want according to the Tag attribute we set, or determine the type of control

3. Read the value we want from the found control.

4. Store the collected values or process them

Here’s how to generate the form

for(int i = 0; i< 5 ; i++) { TableRow row = new TableRow(this); TextView txtView1 = new TextView(this); EditText edit1 = new EditText(this); txtView1.setText("lable"+i+":"); txtView1.setTag("lable"+i); txtView1.setPadding(3, 3, 3, 3); edit1.setTag("text"+i); edit1.setPadding(3, 3, 3, 3); //LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);  row.addView(txtView1); row.addView(edit1); LayoutParams layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); _pnlContent.addView(row,layoutParams2); } _pnlContent.setColumnStretchable(1,true);Copy the code

The above code demonstrates the creation of multiple TableRow controls, which are child controls of the table. We’re going to put a table ayout control on the interface, and then we’re going to dynamically create a TableRow, a TableLayout, a row in a TableRow. That is, we place a static table and then dynamically create rows in the table.

LayoutParams stands for layout parameters. You can specify a layout parameter when you add a created child control to its parent container control. This layout parameter indicates how the child control is rendered in the parent container control.

One of the constructors of LayoutParams takes two parameters, indicating the width and height of the layout, which we can specify

ViewGroup. LayoutParams. WRAP_CONTENT to automatically adapt to their own content size ViewGroup. LayoutParams. FILL_PARENT, Filling the parent container setColumnStretchable is TableRow's method, which indicates that column 2 (column index 1) is stretchable.Copy the code

Read the contents of a control from a view:

We write this code in the click event of a button

StringBuffer sb = new StringBuffer(); for(int i = 0 ; i< _pnlContent.getChildCount(); i++) { TableRow tr = (TableRow)_pnlContent.getChildAt(i); TextView txt1 = (TextView)tr.getChildAt(0); EditText et1 = (EditText)tr.getChildAt(1); sb.append(String.format("{%s}={%s}\n", txt1.getTag().toString(), et1.getText() )); } Toast.makeText(getApplicationContext(), sb.toString(), 0).show();Copy the code

As shown in the code above. We iterate over the container control using the method getChildAt(0); Obtain the specified index of the child control, reasonable transformation for our use. We can also use tags to store our additional information.

Merging of cells

You will also encounter code to set up cell merges as follows:

LayoutParams layoutParams2 = null; layoutParams2 = new LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); // Determine typeDesc type generation control TableRow row = null; Button btnRemove = null; row = new TableRow(getContext()); EditText editText = null; editText = new EditText(getContext()); TableRow.LayoutParams rowSpanLayout = new TableRow.LayoutParams( TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT); rowSpanLayout.span = 2; editText.setLayoutParams(rowSpanLayout); btnRemove = new Button(getContext()); btnRemove.setText("-"); row.addView(editText); row.addView(btnRemove); tablleLayout.addView(row,layoutParams2);Copy the code

Full text, code download