android中的表格布局 - 大小调整

时间:2011-05-01 08:44:31

标签: android

我有一个如图所示的表格布局。我有很多行。 我用android:strechcolumns =“1”来拉伸每一行的第二列。这种情况无处不在。

但是在第五行我有2个字段。我希望它们占用相等的空间。行大小应该与前一行的大小相同。我希望行的整体大小保持与上一行相同。 screeshot.Also我在截图中指出了所有行应该在的边界。

如何做到这一点?

<TableRow>
    <TextView
        android:gravity="right"
        android:paddingTop="10dip"
        />
    <Spinner
    android:id="@+id/depot_name_spinner"/>
</TableRow>

<TableRow>
    <TextView 
     android:text="@string/product_name"
     android:paddingTop="10dip"
     android:gravity="right"/>
    <Spinner
    android:id="@+id/product_name_spinner"
     />
</TableRow>

<TableRow>
    <TextView
        android:text="@string/date"
        android:gravity="right" />
    <Button
    android:id="@+id/date_button" />
</TableRow>

<TableRow>
    <TextView 
        android:text="@string/measure_ltr"
        android:gravity="right"

        />
    <EditText
    android:id="@+id/ltr_text" 
    android:layout_width="50dip"/>
     <TextView 
        android:text="@string/measure_qts"
        android:gravity="right"
     />
      <EditText

    android:id="@+id/qts_text"
     />  
</TableRow>

enter image description here

由于

2 个答案:

答案 0 :(得分:2)

您不仅可以在表格中添加TableRow。只需使用简单的LinearLayout并将所有原始内容放入其中。我认为它必须解决你的问题。

编辑:此外,您可以将LinearLayout所有小部件添加到TableRow并设置LinearLayout的{​​{1}}:

android:layout_span=2

我没试过,但希望它能奏效。

答案 1 :(得分:1)

尝试使用android:weight。一旦布局布局,您将按照您想要的方式划分剩余空间。我的意思是你可以定义每个视图应该使用的空间比例。请看下面这个例子。

<TableLayout android:layout_width="fill_parent" android:layout_height="wrap_content">
    <TableRow>

    <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal"
                    android:layout_height="wrap_content" android:weightSum="100">     // Mention Weightsum as 100 so that its easy to split among your views.

        <TextView android:id="@+id/row1Label" 
            android:layout_width="0dip" android:layout_weight="60"      // This will occupy 60% of the remaining space
            android:layout_height="wrap_content">
        </TextView>

        <TextView android:id="@+id/row1Label" 
            android:layout_width="0dip" android:layout_weight="40"      // This will occupy 40% of the remaining space
            android:layout_height="wrap_content">
        </TextView>

    </LinearLayout>

    </TableRow>

    <TableRow>

    <LinearLayout android:layout_width="fill_parent" android:orientation="horizontal"
                    android:layout_height="wrap_content" android:weightSum="100">

        <TextView android:id="@+id/row2Label" 
            android:layout_width="0dip" android:layout_weight="60"      // This will occupy 60% of the remaining space
            android:layout_height="wrap_content">
        </TextView>

        <TextView android:id="@+id/row3Label" 
            android:layout_width="0dip" android:layout_weight="40"      // This will occupy 40% of the remaining space
            android:layout_height="wrap_content">
        </TextView>


        // You can more views and distribute the space accordingly....

    </LinearLayout>

    </TableRow>

...........

有了这个,你就可以实现这样的目标 enter image description here

您可以以任何方式拆分表格行区域。这可以仅使用线性布局来完成,因为其他布局没有此权重选项。我广泛使用这种方法来创建复杂的视图。希望它可以帮到你。