用户登录
用户注册

分享至

Android UI控件系列:TableLayout(表格布局)

  • 作者: 内涵的文字讲不出笑点丶
  • 来源: 51数据库
  • 2020-08-14

TableLayout是一个以行、列显示视图View的视图组

1、开始一个新的工程,名字叫做HelloTableLayout

2、打开res/layout/main.xml文件并且插入如下内容

<?xml version="1.0" encoding="utf-8"?>
<TableLayout
? ? ? ? xmlns:android="http://schemas.android.com/apk/res/android"
? ? ? ? android:layout_width="fill_parent"
? ? ? ? android:layout_height="fill_parent"
? ? ? ? android:stretchColumns="1">
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:layout_column="1"
? ? ? ? ? ? ? ? ? ? ? ? android:text="Open..."
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Ctrl-O"
? ? ? ? ? ? ? ? ? ? ? ? android:gravity="right"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:layout_column="1"
? ? ? ? ? ? ? ? ? ? ? ? android:text="Save..."
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Ctrl-S"
? ? ? ? ? ? ? ? ? ? ? ? android:gravity="right"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:layout_column="1"
? ? ? ? ? ? ? ? ? ? ? ? android:text="Save as..."
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Ctrl-Shift-S"
? ? ? ? ? ? ? ? ? ? ? ? android:gravity="right"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
? ? ? ? <View
? ? ? ? ? ? ? ? android:layout_height="2dip"
? ? ? ? ? ? ? ? android:background="#FF909090"
? ? ? ? />
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="X"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Import..."
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="X"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Export..."
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:text="Ctrl-E"
? ? ? ? ? ? ? ? ? ? ? ? android:gravity="right"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
? ? ? ? <View
? ? ? ? ? ? ? ? android:layout_height="2dip"
? ? ? ? ? ? ? ? android:background="#FF909090"
? ? ? ? />
? ? ? ? <TableRow>
? ? ? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? ? ? ? ? android:layout_column="1"
? ? ? ? ? ? ? ? ? ? ? ? android:text="Quit"
? ? ? ? ? ? ? ? ? ? ? ? android:padding="3dip"
? ? ? ? ? ? ? ? />
? ? ? ? </TableRow>
</TableLayout>

注意到这个文件类似于HTML的table的结构,TableLayout元素就像是HTML中的<table>元素;TableRow就像是一一个<tr>元素;但是对于每一个单元格,你可以用各种视图元素,在这里例子里,每个单元格用TextView,在这些行之间,还有一个基本View,用来画水平线

TextView中的一些属性

android:layout_column=”1″:表示控件放在标号为1的列上,标号是从0开始的
android:gravity=”right”:定义字体在父控件中显示在右边
android:stretchColumns=”1″:设置自动拉伸哪些列,列ID从0开始,多个列的话用”,”分隔。这里的作用是让第2列可以扩展到所有可用空间
android:shrinkColumns:设置自动收缩哪些列,列ID从0开始,多个列的话用”,”分隔
android:collapseColumns:设置自动隐藏哪些列,列ID从0开始,多个列的话用”,”分隔
顺便:android:layout_span表示一个控件占几列空间
下面的是基本的View,是在屏幕上画一条2dip高的一条横线
<View
android:layout_height=”2dip”
android:background=”#FF909090″
/>

3、运行结果如下:

软件
前端设计
程序设计
Java相关