2

Android之TableLayout(表格布局)

 3 years ago
source link: https://www.jishudog.com/32510/html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

释放双眼,带上耳机,听听看~!

1.本节学习路线图

vQrm2qJ.jpg!mobile

路线图分析:从上面的路线图,可以看出TableLayout的用法还是很简单的,无非就是确定表格的行数,以及使用 那三个属性来设置每一行中的第某列的元素隐藏,拉伸,或者收缩即可!

2.TableLayout的介绍

相信学过HTML的朋友都知道,我们可以通过< table >< tr >< td >就可以生成一个HTML的表格, 而Android中也允许我们使用表格的方式来排列组件,就是行与列的方式,就说我们这节的TableLayout! 但却不像我们后面会讲到的Android 4.0后引入的 GridLayout(网格)布局 一样,直接就可以设置多少行与多少列!

3.如何确定行数与列数

  • ①如果我们直接往TableLayout中添加组件的话,那么这个组件将占满一行!!!
  • ②如果我们想一行上有多个组件的话,就要添加一个TableRow的容器,把组件都丢到里面!
  • ③tablerow中的组件个数就决定了该行有多少列,而列的宽度由该列中最宽的单元格决定
  • ④tablerow的layout_width属性,默认是fill_parent的,我们自己设置成其他的值也不会生效!!! 但是layout_height默认是wrapten——content的,我们却可以自己设置大小!
  • ⑤整个表格布局的宽度取决于父容器的宽度(占满父容器本身)
  • ⑥有多少行就要自己数啦,一个tablerow一行,一个单独的组件也一行!多少列则是看tableRow中 的组件个数,组件最多的就是TableLayout的列数

4.三个常用属性

  • android:collapseColumns: 设置需要 被隐藏 的列的序号
  • android:shrinkColumns: 设置允许 被收缩 的列的列序号
  • android:stretchColumns: 设置运行 被拉伸 的列的列序号
以上这三个属性的列号都是 从0开始算 的,比如shrinkColunmns = “2”,对应的是第三列!可以 设置多个 ,用 逗号隔开 比如”0,2″,如果是所有列 都生效 ,则 用” “号

即可除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:

android:layout_column=”2″:表示的就是

跳过 第二个,直接显示到第三个格子处,从1开始算的! android:layout_span =”4″:表示 合并 *4个单元格,也就说这个组件占4个单元格

属性使用示例:

①collapseColumns(隐藏列)

流程:在TableRow中定义5个按钮后,接着在最外层的TableLayout中添加以下属性: android:collapseColumns = “0,2”,就是隐藏第一与第三列,代码如下:

<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="0,2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
</TableRow>
</TableLayout>

运行效果图:

F3iaAzb.jpg!mobile

②stretchColumns(拉伸列)

流程:在TableLayout中设置了四个按钮,接着在最外层的TableLayout中添加以下属性: android:stretchColumns = “1”

设置第二列为可拉伸列,让该列填满这一行所有的剩余空间,代码如下:

<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</TableRow>
</TableLayout>

运行效果图:

IfA7VrQ.jpg!mobile

③shrinkColumns(收缩列)

步骤:这里为了演示出效果,设置了5个按钮和一个文本框,在最外层的TableLayout中添加以下属性: android:shrinkColumns = “1”

设置第二个列为可收缩列,代码如下:

<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本XX" />
</TableRow>
</TableLayout>

运行截图:

e677323.jpg!mobile

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK