In android you define the number of rows by your own and android determines the number of cells in each row according to the number of widgets in each row
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Submit"
/>
TableLayout tl=new TableLayout(this);This example represents a row with three cells.
TableRow tr=new TableRow(this);
Button btn=new Button(this);
btn.setText("Hello");
tr.addView(btn);
tl.addView(tr);
setContentView(tl);
A table cell can span multiple columns like this:
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Submit"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Cancel"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Submit"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Cancel"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Help"
android_layout_column="1"
/>
Table layout also allows you to put widgets directly under the <TableRow> tag to act as a separator between rows.
These widgets will have their width set to fill_parent.
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Submit"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Cancel"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Help"
android_layout_column="5"
/>
<Button
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_text="Separator"
/>
In table layout each column occupies space equal to the size of the largest widget in it. But you can set the width of any column to take the largest available space, just like setting the width to 100 % in HTML. This is done by setting the property android:stretchColumns to the index of the column, also you can set multiple columns by separating them with a comma.
Look at the layout below:
Now we gonna to add android:stretchColumns="0" to the table layout and see what its gonna look like:
Column 0 occupied the largest available space.
TableLayout tl=new TableLayout(this);the setColumnStretchable(ColumnIndex, IsStretchable)method parameters are the column index and a Boolean value to indicate it is going to be stretched.
tl.setColumnStretchable(0, true);
Now if we have a column that have a large content. Android columns by default do not wrap their content. Look at this layout:
We can use android:shrinkColumns property to wrap content of a certain column or for multiple columns by assigning column numbers separeted by commas.
When we use the property with column zero it will be like this:
You can set the property from code like this:
TableLayout tl=new TableLayout(this);The setColumnShrinkable(columnIndex, isShrinkable) method parameters are the column index and a Boolean to indicate it si going to be shrinked.
tl.setColumnShrinkable(0, true);
Finally if you want to make some columns invisible you can use the property android:collapseColumns the same way we used the last two properties
You can use this property from code like this:
TableLayout tl=new TableLayout(this);The property setColumnCollapsed(columnIndex, isCollapsed) parameters are the column index and a Boolean to indicate that its going to be collapsed
tl.setColumnCollapsed(0, true);
This is just like using the style=display:none; in HTML
Other functions can be called from code:
TableLayout.setShrinkAllColumns(Boolean shrinkAllColumns) : shrinks all colums
TableLayout.setStretchAllColumns(Boolean stretchAllColumns): stretches all columns
0 comments:
Post a Comment