文章目录
1 Android中的表格布局
重要属性:
- android:stretchColumns
- android:shrinkColumns
- android:collapseColumns
效果如下:

xml文件如下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*">
<!--如果直接往TableLayout中添加控件,那么该控件与屏幕等宽
如果向使多个控件在同一行,那么我们在这些控件外层包裹一对TableRow
并且在这种情况下,控件宽度与内容适配
android:stretchColumns="*" 设置可以伸展的列,直接传列的索引,如果有多列,
以,作为分割,*表示全部
android:shrinkColumns 设置可以缩小的列
android:collapseColumns="1" 设置可隐藏的列
-->
<EditText android:layout_weight="2"
android:text="0"
android:gravity="right|center_vertical"
android:textSize="28sp"
/>
<TableRow android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:text="C"
android:textSize="24sp" />
<Button
android:layout_height="match_parent"
android:text="+/-"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="%"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="÷"
android:textSize="24sp"/>
</TableRow>
<TableRow android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:text="7"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="8"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="9"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="*"
android:textSize="24sp"/>
</TableRow>
<TableRow android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:text="4"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="5"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="6"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="-"
android:textSize="24sp"/>
</TableRow>
<TableRow android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:text="1"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="2"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="3"
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="+"
android:textSize="24sp"/>
</TableRow>
<TableRow android:layout_weight="1">
<Button
android:layout_height="match_parent"
android:text="0"
android:textSize="24sp"
android:layout_span="2"/>
<Button
android:layout_height="match_parent"
android:text="."
android:textSize="24sp"/>
<Button
android:layout_height="match_parent"
android:text="="
android:textSize="24sp"/>
</TableRow>
</TableLayout>
本文介绍了Android中TableLayout的使用,包括重要属性android:stretchColumns、android:shrinkColumns和android:collapseColumns的详细解释,并通过一个计算器布局的示例展示了如何创建和调整表格布局,以实现屏幕适配和功能布局。
548

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



