android-TableLayout以及TableRow的使用
2016-12-21 09:49
435 查看
android-TableLayout以及TableRow的使用
TableLayout是一种可以制作表格的布局,它和GridLayout的区别是GridLayout只能制定每一列宽度一样的表格布局,而TableLayout能够制定各列宽度不一样的表格布局。
TableLayout的主要属性:
android:collapseColumns=”0,1” 隐藏第0列和第1列
android:stretchColumns=”0,1” 第0列和第1列可以向行方向扩展
android:stretchColumns=”*” 所有列可以向行方向扩展
android:shrinkColumns=”0,1” 第0列和第1列可以向列方向扩展
TableRow的子控件的主要属性:
android:layout_column=”1” 该控件显示在第1列
android:layout_span=”2” 该控件占据2列
注:TableLayout里面的子控件可以为TableRow或者其他View,但是其子控件的android:layout_width属性被系统固定为match_parent,TableLayout里面所有行中某一列的宽度的最大值是这一列的宽度。
代码示例
运行结果:
TableLayout是一种可以制作表格的布局,它和GridLayout的区别是GridLayout只能制定每一列宽度一样的表格布局,而TableLayout能够制定各列宽度不一样的表格布局。
TableLayout的主要属性:
android:collapseColumns=”0,1” 隐藏第0列和第1列
android:stretchColumns=”0,1” 第0列和第1列可以向行方向扩展
android:stretchColumns=”*” 所有列可以向行方向扩展
android:shrinkColumns=”0,1” 第0列和第1列可以向列方向扩展
TableRow的子控件的主要属性:
android:layout_column=”1” 该控件显示在第1列
android:layout_span=”2” 该控件占据2列
注:TableLayout里面的子控件可以为TableRow或者其他View,但是其子控件的android:layout_width属性被系统固定为match_parent,TableLayout里面所有行中某一列的宽度的最大值是这一列的宽度。
代码示例
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorBlack" android:orientation="vertical"> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent"> <!--android:layout_width="wrap_content"不会起作用--> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="父match_parent,子wrap_content"/> </TableLayout> <TableLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="父wrap_content,子wrap_content"/> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="父match_parent,子match_parent"/> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="正常情况:"/> <!--android:layout_width="wrap_content"不会起作用--> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="normal2"/> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:collapseColumns="0"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="测试列隐藏:"/> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="normal2"/> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:stretchColumns="0"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="测试列扩展:"/> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="normal2"/> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" android:shrinkColumns="0"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="测试列收缩:"/> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal0normal0normal0normal0normal0normal0normal0normal0normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="normal2"/> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="测试单元格属性:"/> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="normal2"/> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_column="1" android:text="我在第一列"/> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorPrimary"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_span="2" android:text="我可以占据两列"/> </TableRow> </TableLayout> <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:stretchColumns="*"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/colorWhite" android:text="制作表格:"/> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="1dp" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:gravity="center" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:gravity="center" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:gravity="center" android:text="normal2"/> </TableRow> <TableRow android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="1dp" android:paddingRight="1dp" android:paddingBottom="1dp" android:background="@color/colorPrimary"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:gravity="center" android:text="normal0"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:layout_marginRight="1dp" android:gravity="center" android:text="normal1"/> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorWhite" android:gravity="center" android:text="normal2"/> </TableRow> </TableLayout> </LinearLayout>
运行结果:
相关文章推荐
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- android学习——使用TableLayout动态生成表格,并为tablerow中的列添加点击监听
- Android 入门——ConstraintLayout详解以及使用替代你的常规布局
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- Android FlexboxLayout的简单了解以及使用
- AndroidUI之ViewPagerIndicator与tableLayout使用流程
- 【Android UI】使用RelativeLayout与TableLayout实现登录界面
- Android TableLayout的使用
- Android SwipeRefreshLayout结合WebView使用实现以及可能遇到的问题
- android 中使用TableLayout实现表单布局效果示例
- Android使用TableLayout动态布局实例
- Android开发-使用TableLayout动态显示表格
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- android 开发零起步学习笔记(十五):android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- android: 静态XML和动态加载XML混合使用,以及重写Layout控件
- [置顶] Android TabLayout使用以及自定义TabLayout的item
- Android 布局详解 -三表格布局(TableLayout)以及重要属性
- Android 对Layout_weight属性完全解析以及使用ListView来实现表格
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(一)
- android学习-----使用TabLayout实现Tab选项卡以及遇到的一些问题(二)