表格布局继承自LinearLayout,通过TableRow设置行,列数有TableRow的子空间来决定。直接添加TableRow 会占据一行。
TableLayout属性:
android:shrinkColumns:设置可收缩的列,内容过多就收缩显示到第二行
android:stretchColumns:设置可伸展的列,将空白区域填充满整个列
android:collapseColumns:设置要隐藏的列
列的索引从0开始,shrinkColumns和stretchColumns可以同时设置。
子控件常用属性:
android:layout_column:第几列
android:layout_span:占据列数
效果图:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="首页"
android:textSize="20sp"
android:textColor="@android:color/holo_blue_light"
android:gravity="center"
android:layout_weight="1"/>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:stretchColumns="0,1,2"
android:layout_weight="10"
android:gravity="center">
<TableRow>
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"
android:text="文件管理"
android:layout_margin="5dp"
android:gravity="center"
/>
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"
android:text="文件管理"
android:layout_margin="5dp"
android:gravity="center"
/>
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"
android:text="文件管理"
android:layout_margin="5dp"
android:gravity="center"
/>
</TableRow>
<TableRow>
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"
android:text="文件管理"
android:layout_margin="5dp"
android:gravity="center"
/>
<TextView android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_dark"
android:text="文件管理"
android:layout_margin="5dp"
android:gravity="center"
android:layout_span="2"
/>
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_gravity="center_vertical"
android:stretchColumns="0,1,2,3">
<TableRow android:orientation="vertical">
<TextView android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="首页"
android:gravity="center"/>
<TextView android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="首页"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="首页"
android:gravity="center"/>
<TextView
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_weight="1"
android:text="首页"
android:gravity="center"/>
</TableRow>
</TableLayout>
</LinearLayout>
本文详细介绍了Android中TableLayout的使用方法及属性,包括如何通过TableRow设置行和列,以及如何使用android:shrinkColumns、android:stretchColumns等属性调整列的显示效果。
893

被折叠的 条评论
为什么被折叠?



