2013-12-23 24 views
0

我试图用TableLayout中的四个TextView来实现40%-20%-20%-20%的分割(或与之相近的东西)。layout_weight在TableLayout中没有兑现

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

enter image description here
红色标注的文字应该两行进行分割。

我即使在最左边的列试android:maxLines="1"在右侧列,android:lines="2",并仍intented将无法正常工作(→有只有一条线路好的,但最右边的列会被切去)。

我也试过this suggestion并在最左边的列上设置了android:layout_width="0dp"。 那么,这给了我一个相反的问题:该列太少空间(长标题被裁剪),而右侧3太多。与android:layout_width="0dp"

我不想硬编码任何DP宽度

enter image description here
相反的问题重视
为列,以更好地支持不同的屏幕。对我来说,指定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> 
+0

我建议使用一个网格布局(是网格布局不GridView的),而不是TableLayout。 – GareginSargsyan

+0

其实,我发现一个简单的解决方案,而坚持使用TableLayout:http://stackoverflow.com/a/20751799/56285 – Jonik

+0

学习使用GridLayout将使它值得你的wile。它比TableLayout更灵活。 – GareginSargsyan

回答

0

那么,作为是典型的,我找到答案我在问这里之后不久。

我需要的是:

android:shrinkColumns="0" 

TableLayoutshrinkColumns属性告诉布局哪些列有资格收缩。 (基于零的索引,因此0意味着最左边一列,你可以指定逗号几列作为分隔符:1, 2, 5

found the answer here(但我不知道这是否有资格作为确切一式两份)。

编辑我结束了扔掉所有从TextViews的layout_weight属性,因为他们似乎没有任何效果。列上的layout_width="wrap_content"也是不必要的。只需shrinkColumns="0"在布局上做我想要的。

编辑2:加回layout_weight的每一列(0.4,0.2,0.2,0.2),否则景观,有足够的空间,行不会使用所有可用的水平空间。

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>