4

我必须设计 UI 以包含两列。

第一列应该包含标签,同一行中的第二列应该填充整个空间。下面是一些例子:

http://temp.chrzanowski.info/so_layout.png

我怎样才能做到这一点 ?

4

4 回答 4

6

TableLayout与_strechColumns = "1"

它拉伸占据剩余空间的第二列。

<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="1" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </TableRow>

</TableLayout>
于 2012-07-18T10:20:31.327 回答
2

以下TableLayout将为您提供所需的输出:

<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
   <TableRow>
      <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="Bla bla bla"
           android:singleLine="true" />            <!-- first column's view -->

      <EditText
           android:layout_width="fill_parent"
           android:layout_height="wrap_content" /> <!-- second column's view -->
   </TableRow>

   ...

</TableLayout>
于 2012-07-18T10:08:52.143 回答
0

作为替代方案,您可以像这样执行 LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="100"
    android:gravity="center_vertical">
    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="40">
        <TextView
            android:layout_weight="1"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:text="A very very very long text"
            android:singleLine="true"
            android:scrollHorizontally="true"
            android:ellipsize="end">
        </TextView>
    </LinearLayout>
    <LinearLayout
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="60">
        <EditText
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="TextBox">
        </EditText>
    </LinearLayout>
</LinearLayout>

要调整列的宽度,请修改layout_weight每个 LinearLayout 容器的。较小的值意味着较小的宽度,并且两个权重必须等于 100。

于 2012-07-18T10:27:19.610 回答
-1

试试这个:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >



        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical">


            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Label 1"
                android:layout_marginLeft="7dp"
                android:layout_marginTop="20dp"/>
            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Label Loooong2"
                android:layout_marginLeft="7dp"
                android:layout_marginTop="30dp"/>
            <TextView 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Label 3"
                android:layout_marginLeft="7dp"
                android:layout_marginTop="30dp"/>
        </LinearLayout>

        <LinearLayout 
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:orientation="vertical">

            <EditText 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="TextBox"/>
            <EditText 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="TextBox"/>
            <EditText 
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:hint="TextBox"/>
        </LinearLayout>


</LinearLayout>
于 2012-07-18T10:21:10.640 回答