layout_weight在TableLayout中没有得到尊重

时间:2013-12-23 21:46:53

标签: android android-layout android-tablelayout

我正在尝试使用TableLayout中的四个TextView实现40% - 20% - 20% - 20%的分割(或接近该分割)。

问题是,最左边的列占用太多空间。换句话说,右边的3列不占用他们需要的空间。 如果需要,左侧较长的文字应分为多行,而不是右侧3个较短的文字。

enter image description here
标有红色的文字应该分成两行。

我甚至在右侧列上尝试了android:maxLines="1",在最左边的列上尝试了android:lines="2",但仍然无法按意图工作(→只有一行正常,但最右边的列获取限幅)。

我还尝试了this suggestion并在最左边的列上设置了android:layout_width="0dp"。 好吧,这给了我相反的问题:该列需要太少空间(长标题被剪辑),而右边的3需要太多。

enter image description here
android:layout_width="0dp"

的相反问题

我不想对列的任何DP宽度值进行硬编码,以便最好地支持不同的屏幕。对我来说,用layout_weight指定相对宽度听起来正是我需要的,只要我让它工作......任何想法我做错了什么?

Android 4.4.2 。我的测试设备是Nexus 7,我在纵向模式下遇到了这个问题。

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />

一行的布局XML(对应于上面的第一个屏幕截图):

<TableRow
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    >

    <TextView
        android:id="@+id/title"
        android:layout_height="wrap_content"
        android:layout_weight="0.4"
        android:gravity="center_vertical|left"
        android:textSize="16dp"
        android:textStyle="bold"

        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        />

    <TextView
        android:id="@+id/rating"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:text="(todo)"
        android:textSize="16dp"
        android:layout_marginTop="8dp"
        />

    <TextView
        android:id="@+id/pages_read"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center"
        android:textSize="16dp"
        android:maxLines="1"

        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        />

    <TextView
        android:id="@+id/date_time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0.2"
        android:gravity="center_vertical|right"
        android:textSize="16dp"

        android:layout_marginTop="8dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="10dp"
        />

</TableRow>

周围TableLayoutScrollView

 <ScrollView
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

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

         <!-- table rows added dynamically in code -->

     </TableLayout>    
 </ScrollView>

2 个答案:

答案 0 :(得分:0)

嗯,正如典型的那样,我在问这里后很快就找到了答案。

我需要的是:

android:shrinkColumns="0"
<{1>}上的

shrinkColumns属性告诉布局哪些列有资格收缩。 (从零开始的索引,因此TableLayout表示最左边的列;您可以使用逗号指定多个列作为分隔符:0。)

found the answer here(但我不确定这是否符合完全重复)。

编辑我最终丢弃了TextViews中的所有1, 2, 5属性,因为它们似乎没有任何效果。另外{{1在列上变得不必要了。布局上的layout_weight就可以实现我的目标。

编辑2 :将layout_width="wrap_content"添加回每列(0.4,0.2,0.2,0.2),否则 in landscape ,房间,行不会使用所有可用的水平空间。

答案 1 :(得分:0)

使用此xml代码:

<TableRow
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
>

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.4"
    android:gravity="center_vertical|left"
    android:textSize="16dp"
    android:textStyle="bold"

    android:layout_marginTop="8dp"
    android:layout_marginBottom="8dp"
    android:layout_marginLeft="10dp"
    />

<TextView
    android:id="@+id/rating"
      android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center"
    android:text="(todo)"
    android:textSize="16dp"
    android:layout_marginTop="8dp"
    />

<TextView
    android:id="@+id/pages_read"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center"
    android:textSize="16dp"
    android:maxLines="1"

    android:layout_marginTop="8dp"
    android:layout_marginLeft="5dp"
    />

<TextView
    android:id="@+id/date_time"
       android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="0.2"
    android:gravity="center_vertical|right"
    android:textSize="16dp"

    android:layout_marginTop="8dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="10dp"
    />

   </TableRow>