..
The layout is a very special TableLayout that lets you organize the contents as if we were working with a table. An example of application that could exploit this type of layout can be offered by a gallery where you might think I showed you a series of thumbnails arranged in an orderly manner using image TableLayout a larger image and then clicked user.
The contents are organized in a TableLayout thanks to a TableRow that identifies a row in the table. Within this object we put all the elements that we want to show that specific row.
We show an illustrative example now going to create a TableLayout with four rows and four columns. The Java code to create it is as follows (again for convenience we import andorid.widget .*):
TableLayout tableLayout TableLayout = new (this);
tableLayout.setGravity (Gravity.TOP);
int count = 0;
for (int i = 0; i <4; i + +) {
TableRow TableRow = new TableRow (this);
tableRow.setGravity (Gravity.CENTER);
for (int j = 0 j <4; j + +) {
count + +;
TextView testoCella = new TextView (this);
testoCella.setText ("cell" + count);
testoCella.setPadding (10, 10, 10, 10);
tableRow.addView (testoCella);
}
tableLayout.addView (TableRow);
}
setContentView (tableLayout);
We are now going to do a rigorous analysis of the code.
First you create an object of type TableLayout which is positioned at the top of the screen.
The construction of the layout itself is performed using two nested for.
For in the first declare an object of type TableRow, and gravity we set the property to center the content within the object.
For each iteration of the second for let's fill the tabeRow with a TextView (we use the variable count incremented on each iteration), then apply a padding of 10 pixels from the four edges of the cell and finally to associate the TextView TableRow created for the primary.
At the end of the first add for the TableRow (containing all four of our TextView created with nested loop) to TableLayout.
Finally, with the usual command setContentView, we associate the TableLayout created the window of our activity
The end result is shown in the screenshot below:

In addition to the two layouts seen previously there are two more easier to use: FrameLayout and RelativeLayout. The first layout is a basic fact may contain only one widget and extends throughout the largest available from the point at the top left of the screen. The second is the element position in the window with its original size and, therefore, without doing any resizing.
Obviously, in a window, you can use more than one type of layout, remembering, however, then incorporate them into a single layout that will be passed as argument to the method setContentView. An example of this is shown in the following code:
LinearLayout linearLayout1 LinearLayout = new (this); / / Code for inserting elements for this layout TableLayout tableLayout TableLayout = new (this); / / Code for inserting elements for this layout LinearLayout linearLayoutPrimario LinearLayout = new (this); linearLayoutPrimario.setOrientation (LinearLayout.VERTICAL); linearLayoutPrimario.addView (linearLayout1); linearLayoutPrimario.addView (tableLayout); setContentView (linearLayoutPrimario);
| |
Flash MX (Advanced)
Become a designer of Web sites from 29 €. |
| |
Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €. |
| |
Webmaster Base (First)
Create a Web site from scratch. Starting from 29 €. |