3.TableLayout:Android 的TableLayout的布局就像Html的表格一样,可以根据我们的说明来安排widgets的位置。我们可以自己控制屏幕的行数和列数,而每列可以根据包含的内容进行伸缩。
通常情况下,TableLayout有多个TableRow组成,每个TableRow就是一行,定义几个TableRow就是定义几行。TableLayout不会显示行或者列或者cell的边线。TableLayout的行数是由我们自己声明的,列数也是由我们间接控制的。最长的行中的每个 widget至少跨越一列,如果我们定义了三行,一行有两个widgets,一行有三个widgets,还有一个有四个,则该布局至少有四列。 我们也可以通过设置属性android:layout_span来声明一个widget跨越的列数。下面的例子标识EditView widget跨越了三列。
<TableRow>
<TextView android:text=”URL:” />
<EditText
android:id=”@+id/entry”
android:layout_span=”3″/>
</TableRow>
TableLayout的每列可以根据包含的内容进行伸缩。这个是通过 TableLayout的属性android:stretchColumns来设置的,该属性的值可以设置单个列数,或者是由逗号分隔的列数组合。这些列自动扩展来占据行中可用的空间。相反的,我们可以通过设置android:shrinkColumns来word-wrap 列包含的内容,以达到压缩列的有效宽度。我们也可以通过设置android:collapseColumns 属性来控制列的可见与否,这在用户选择显示重要信息,屏蔽不重要信息的时候可以应用。可以在代码中调用setColumnStretchable() 和setColumnShrinkable()、setColumnCollapsed() 。
注意:列数是从0开始计数的。
<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:stretchColumns=”4″>
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”hello” />
<EditText android:id=”@+id/entry” android:text=”world”
android:layout_span=”3″ />
<EditText android:text=”this is a test” android:layout_span=”2″ />
/>
</TableRow>
<TableRow>
<Button android:id=”@+id/cancel” android:layout_column=”2″
android:text=”Cancel” />
<Button android:id=”@+id/ok” android:text=”OK”
android:layout_column=”5″ />
</TableRow>
</TableLayout>
运行效果如下:
在看如下代码:
<TableLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”
android:stretchColumns=”4″
android:collapseColumns=”2″>//隐藏第三列
<TableRow>
<TextView android:layout_width=”wrap_content”
android:layout_height=”wrap_content” android:text=”hello” />
<EditText android:id=”@+id/entry” android:text=”world”
android:layout_span=”3″ />
<EditText android:text=”this is a test” android:layout_span=”2″ />
/>
</TableRow>
<TableRow>
<Button android:id=”@+id/cancel” android:layout_column=”2″
android:text=”Cancel” />
<Button android:id=”@+id/ok” android:text=”OK”
android:layout_column=”5″ />
</TableRow>
</TableLayout>
4.ScrollView:提供滚动条的 Container。
<ScrollView
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”>
<TableLayout
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:stretchColumns=”0″>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#000000″/>
<TextView android:text=”#000000″
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#440000″ />
<TextView android:text=”#440000″
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#884400″ />
<TextView android:text=”#884400″
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#aa8844″ />
<TextView android:text=”#aa8844″
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#ffaa88″ />
<TextView android:text=”#ffaa88″
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#ffffaa” />
<TextView android:text=”#ffffaa”
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
<TableRow>
<View
android:layout_height=”80px”
android:background=”#ffffff” />
<TextView android:text=”#ffffff”
android:paddingLeft=”4px”
android:layout_gravity=”center_vertical” />
</TableRow>
</TableLayout>
</ScrollView>
运行效果如下: