TableLayout一些XMl属性

摘录自郭神的《第一行代码》

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Account:" />
<EditText
android:id="@+id/account"
android:layout_height="wrap_content"
android:hint="Input your account" />
</TableRow>
<TableRow>
<TextView
android:layout_height="wrap_content"
android:text="Password:" />
<EditText
android:id="@+id/password"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</TableRow>
<TableRow>
<Button
android:id="@+id/login"
android:layout_height="wrap_content"
android:layout_span="2"
android:text="Login" />
</TableRow>
</TableLayout>

在 TableLayout 中每加入一个 TableRow 就表示在表格中添加了一行,然后在 TableRow中每加入一个控件,就表示在该行中加入了一列,TableRow 中的控件是不能指定宽度的。
这里我们将表格设计成了三行两列的格式,第一行有一个 TextView 和一个用于输入账号的EditText , 第 二 行 也 有 一 个 TextView 和 一 个 用 于 输 入 密 码 的 EditText , 我 们 通 过 将android:inputType 属性的值指定为 textPassword,把 EditText 变为密码输入框。可是第三行只有一个用于登录的按钮,前两行都有两列,第三行只有一列,这样的表格就会很难看,而且结 构 也 非 常 不 合 理 。 这 时 就 需 要 通 过 对 单 元 格 进 行 合 并 来 解 决 这 个 问 题 , 使 用
android:layout_span=”2”让登录按钮占据两列的空间,就可以保证表格结构的合理性了。
TableLayout一些XMl属性_第1张图片

当前的登录界面并没有充分利用屏幕的宽度,右侧还空出了一块区 域 , 这 也 难 怪 , 因 为 在 TableRow 中 我 们 无 法 指 定 控 件 的 宽 度 。 这 时 使 用android:stretchColumns 属性就可以很好地解决这个问题,它允许将 TableLayout 中的某一列
进行拉伸,以达到自动适应屏幕宽度的作用。修改 activity_main.xml 中的代码,如下所示:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1"
>
……

TableLayout一些XMl属性_第2张图片
这里将 android:stretchColumns 的值指定为 1,表示如果表格不能完全占满屏幕宽度,就将第二列进行拉伸。没错!指定成 1 就是拉伸第二列,指定成 0 就是拉伸第一列,不要以为这里我写错了哦。

你可能感兴趣的:(TableLayout一些XMl属性)