Android:在表单中对齐标签

sle*_*ica 2 android android-layout

创建表单布局的最简单方法是什么,例如(比如说)这个:

.-----------------------------------------------.
|          First Field [    (EditText)    ]     |
|     Some Other field [    (EditText)    ]     |
|        A Third Field [    (EditText)    ]     |
.-----------------------------------------------.
Run Code Online (Sandbox Code Playgroud)

我的Android有点生疏,无法弄清楚如何正确使用=(.

细节:

  1. 整件事以集装箱为中心.

  2. 标签在它们之间水平右对齐,并与它们各自的输入框垂直居中对齐(虽然我无法在上面的纯文本中表示)

  3. 输入框的大小都相同.

如果EditTexts可以收缩或增长到最大宽度,保持布局如上所示:D,以便处理方向更改而不会出现丑陋的超大输入.

dca*_*121 6

试试这个布局,

您应该始终考虑将TableLayout用于此类设计而不是RelativeLayout,因此您可以轻松地动态添加任意数量的行.

<TableLayout
            android:id="@+id/mainTable"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dip"
            android:layout_marginRight="5dip"
            android:paddingLeft="3dip"
            android:paddingRight="3dip"
            android:shrinkColumns="1"
            android:stretchColumns="*"
            android:visibility="gone" >

            <TableRow>

                <TextView
                    style="@style/ds_20_b_darkgray"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Label1: " >
                </TextView>

                <EditText
                    android:id="@+id/editT1"
                    style="@style/ds_20_b_black"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right" >
                </EditText>
            </TableRow>

            <TableRow>

                <TextView
                    style="@style/ds_20_b_darkgray"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Label2: " >
                </TextView>

                <EditText
                    android:id="@+id/editT2"
                    style="@style/ds_20_b_black"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:gravity="right" >
                </EditText>
            </TableRow>
      </TableLayout> 
Run Code Online (Sandbox Code Playgroud)

您可能需要将整个表格布局放在linearlayout或RelativeLayout中,并使表格布局位于中心并根据需要调整边距.

如果这不符合您的要求,我可以修改它.