表格布局管理器用TableLayout类表示,该类派生自LinearLayout类,所以TableLayout类也具有LinearLayout类的方法和属性。
线性布局管理器LinearLayout将在其中的“组件群”进行横向或者纵向的一字排列。而表格布局管理器TableLayout主要将“组件群”进行表格式的排列,即将“组件群”排列成指定行数和指定列数。
1 在表格布局管理器中插入行
在表格布局管理器中,每一个控件表示表格中的每一行。在《从零开始学android编程之线性布局管理器》中提到的activity_linear.xml文件中使用表格布局管理器TableLayout,代码如下
android:orientation="horizontal"
.........
>
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一行按键1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:text="第二行按键1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第三行按键1"
/>
在线性布局管理器中添加了一个表格布局管理器TableLayout
,在表格布局管理器中添加了三个
Button
组件,每个组件代表该表格中的一列,其效果如图
1
所示。
从以上代码中可以看出,虽然线性布局管理器的android:orietation属性的值是horizontal,但是表格布局管理器中组件的排列方式不受影响。
2 在行中插入列
“1 在表格布局管理器中插入行”中创建的是3×1的表格。可以通过TableRow的方式创建包含多个列的表格。TableRow表示表格中的一行,而TableRow中的组件表示该行中包含的列数,代码如下所示。
.........
>
android:layout_width="wrap_content"
android:layout_height="wrap_content">
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一行按键1"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一行按键2"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一行按键3"/>
..........
其中,TableRow表示表格布局管理器的第1行,在该行中又包含3列,其效果如图2所示。从图2中可以看到,在第1行添加了3列之后,第2行和第3行Button组件的宽度自动展宽,保持了表格的形状。如果需要为第2行添加列,则在第2行中只用TableRow即可。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/hz/142077.html