Android(安卓)TableLayout 表格布局
Android TableLayout (表格布局)将按行和列排列视图组。您将使用 <TableRow> 元素在表中构建一行。每行有零个或多个单元格;每个单元格可以容纳一个 View 对象。
TableLayout 容器不显示其行、列或单元格的边框线。

TableLayout 属性
以下是 TableLayout 特有的重要属性:
| 编号 | 属性 & 描述 |
|---|---|
| 1 | android:id 这是唯一标识布局的 ID。 |
| 2 | android:collapseColumns 这将指定要折叠的列的从 0 开始的索引列索引必须用逗号分隔:1、2、5。 |
| 3 | android:shrinkColumns 要收缩的列的从 0 开始的索引列索引必须用逗号分隔:1、2、5。 |
| 4 | android:stretchColumns 要拉伸的列的从 0 开始的索引列索引必须用逗号分隔:1、2、5。 |
实例
这个例子将带你通过简单的步骤来展示如何使用 TableLayout 创建你自己的 Android 应用程序。按照以下步骤修改我们在 Hello World 实例 章节中创建的 Android 应用程序:
| 步骤 | 描述 |
|---|---|
| 1 | 您将使用 Android Studio IDE 创建一个 Android 应用程序,并将其命名为 com.example.demo 包下的 demo。如 Hello World 实例一章所述。 |
| 2 | 修改 res/layout/activity_main.xml 文件的默认内容以在表格布局中包含几个小部件。 |
| 3 | 无需修改 string.xml,Android Studio 负责默认常量 |
| 4 | 运行应用程序以启动 Android 模拟器并验证应用程序中所做更改的结果。 |
下面是修改后的主 activity 文件 src/com.example.demo/MainActivity.java 的内容。该文件可以包括每个基本的生命周期方法:
package com.example.demo;import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}}
下面是 res/layout/activity_main.xml 文件的内容:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"><TableRowandroid:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:text="Time"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="1" /><TextClockandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/textClock"android:layout_column="2" /></TableRow><TableRow><TextViewandroid:text="First Name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="1" /><EditTextandroid:width="200px"android:layout_width="wrap_content"android:layout_height="wrap_content" /></TableRow><TableRow><TextViewandroid:text="Last Name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_column="1" /><EditTextandroid:width="100px"android:layout_width="wrap_content"android:layout_height="wrap_content" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="fill_parent"><RatingBarandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/ratingBar"android:layout_column="2" /></TableRow><TableRowandroid:layout_width="fill_parent"android:layout_height="fill_parent"/><TableRowandroid:layout_width="fill_parent"android:layout_height="fill_parent"><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Submit"android:id="@+id/button"android:layout_column="2" /></TableRow></TableLayout>
下面是 res/values/strings.xml 文件的内容,其中定义了两个常量:
<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">HelloWorld</string><string name="action_settings">Settings</string></resources>
让我们尝试运行我们修改过的 Hello World!我们刚刚修改的应用程序。我假设您在进行环境设置时创建了 AVD。要从 Android Studio 运行应用程序,请打开项目的 activity 文件之一,然后单击工具栏上的运行 Eclipse
图标。Android studio 在您的AVD 上安装应用程序并启动它,如果您的设置和应用程序一切正常,它将显示以下模拟器窗口:
