动态增加TableLayout的行
Just like HTML Tables on webpages the TableLayout on Android gives you the option to align Views in a table order with rows and columns.
My development setup is:IDE: Eclipse IDE (setup guide here).
Phone: HTC Hero
Android version: 1.5 HTC Rom
I will be using the standard Android Project Skeleton in Eclipse as my base setup. This auto creates all the basic files needed, so these will not be covered here.
TableLayout on AndroidThe TableLayout is built using the TableLayout and the TableRow commands. There is no TableCols like the <td> tag in HTML. To align your view in columns you have to set the width of the elements and manually control the layout.
XML LayoutTo make a basic layout with 2 rows and 4 textview I have a main.xml with this content:
So we have now displayed a simple TableLayout.
Right aligning checkboxesMany apps have a nice clean setup where the checkboxes is right aligned to the screen and this is actually very easy to achieve. You just have to set the width of the textview so it will push the checkbox to the right.
First step is to change the TextView02 to a Checkbox like this:
Now we just have to align the checkbox.
We can do this by setting the width of the previous textfield like this:
But what is the screen size changes? The checkbox will then be placed 250px from the left because the width is hardcoded. Not that great but yet very easy to adjust.
On the LayoutTable we can adjust stretching across columns, similar to the colspan tag in HTML.
And with the screen flipped:
Complete XML layout:
So whenever the button is clicked, it adds a new row to the TableLayout and notice the Scrollbar from the ScrollView.
If you want more information and TableLayout and placement techniques I can recommend reading:
Thats it for now. Enjoy coding and please post some comments.
原文:http://huuah.com/using-tablelayout-on-android/