TableRow比通常的android TableLayout占用更多空间

时间:2012-09-18 17:02:24

标签: android android-layout

美好的一天,我遇到了下面这张图片的麻烦。正如您所看到的,表行占用了大量空间,我想减少它们,因为它们下面还有其他视图小部件。我怎么能实现这个目标?谢谢。

enter image description here

这是我的xml:

<?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="vertical">

    <TableLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:weightSum="3"
    android:padding="8dp"
    android:background="@drawable/rounded_date_filter">

  <TableRow
      android:orientation="horizontal"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_weight="1">

 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/from_text">
</TextView>
<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="01-01-2012">
</TextView>
</TableRow>

  <View
      android:layout_width="fill_parent"
      android:layout_height="1px"
      android:background="#606060">
  </View>

<TableRow>
 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/to_text">
</TextView>

<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="01-09-2012">
</TextView>
</TableRow>

      <View
      android:layout_width="fill_parent"
      android:layout_height="1px"
      android:background="#606060">
  </View>

    <TableRow>
 <TextView
  android:id="@+id/datefilter_text_from_id"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:layout_weight="1"
  android:layout_marginLeft="10dp"
  android:layout_marginTop="10dp"
  android:text="@string/groupby">
</TextView>

<TextView
  android:id="@+id/datefilter_text_to_id"
  android:layout_width="10dp"
  android:layout_height="wrap_content"
  android:layout_weight="1"
  android:layout_marginLeft="40dp"
  android:layout_marginTop="10dp"
  android:text="day">
</TextView>
</TableRow>

</TableLayout>

    <Button
        android:id="@+id/reset_filter_button_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/reset_filters">
    </Button>
</LinearLayout>

3 个答案:

答案 0 :(得分:1)

我测试了您的布局,最后看来weightSumlayout_weight不负责任。设置TableLayout以包装内容和layout_weight TextViews似乎扩展了TableLayout并使其具有您看到的行为。

如果您将TableLayout的宽度设置为FILL_PARENT,问题就会消失:

// ...
<TableLayout 
    android:layout_width="fill_parent"
// ...

当你尝试使用布局权重时,当你没有真正的空间(因为wrap_content)时,这种方式是有道理的,因此没有什么好处。

此外,如果这是您的所有布局,您可以对其进行改进并将Button直接添加到TableLayout(在另一个TableRow中),从而无需使用包装{{1 }}

答案 1 :(得分:0)

layout_weight用于告诉布局强制填充空间。在上面的布局中,TableRow权重指定为1,weighSum指定为3.删除它将起作用的layout_weight和weightSum。

答案 2 :(得分:0)

以下是更正后的布局文件:

<?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="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="8dp"
        android:weightSum="3" >

        <TableRow
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="From" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="01-01-2012" >
            </TextView>
        </TableRow>

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:layout_weight="1"
                android:background="#606060" >
            </View>
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="To" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="01-09-2012" >
            </TextView>
        </TableRow>

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

            <View
                android:layout_width="fill_parent"
                android:layout_height="1px"
                android:layout_weight="1"
                android:background="#606060" >
            </View>
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/datefilter_text_from_id"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Group By" >
            </TextView>

            <TextView
                android:id="@+id/datefilter_text_to_id"
                android:layout_width="10dp"
                android:layout_height="wrap_content"
                android:layout_marginLeft="40dp"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                android:text="day" >
            </TextView>
        </TableRow>
    </TableLayout>

    <Button
        android:id="@+id/reset_filter_button_id"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="My Button" >
    </Button>

</LinearLayout>

和输出布局:

enter image description here

我注意到并因此纠正的问题是:

  • row separation添加为单独的行,而不是TableLayout
  • 中的含糊不清的视图
  • layout_width的{​​{1}}从TableLayout更改为wrap_content
相关问题