2012-12-07 21 views
4

假设我有一个的LinearLayout(宽度&高度match_parent)与weightSum = “5”,然后在水平方式为什么布局不符合指定的重量?

我把5 TableLayout(宽度&高度WRAP_CONTENT)与layout_weight =“1”,所以每个TableLayout都是其父项的1/5。

但是,之后,在每个TableLayout我把TextView的(宽度&高度WRAP_CONTENT)和每一件事情不顺心(从我的角度来看):

如果文本是长则强制其父级布局(在我们的示例中为TableLayout)到费用,因此即使指定了TextView的ellipsize =“end”,TableLayout从其LinearLayout父级的1/5比例也不再受到尊重。

好做一些可测量类后,我发现,设置layout_width比所有其他属性(如weightSum)更强

在我来说,我设置TableLayout的宽度= WRAP_CONTENTlayout_weight =“1”,而是因为它是WRAP_CONTENT,它往往其内容后,花费和不尊重的重量。

任何人都可以给我一些解决方案,我的表格布局如何尊重重量?(因为我不希望我的布局不遵守儿童文本视图长度后的比例和支出)。

谢谢你的好意。这里是我的代码:

<!-- my code --> 
< LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:weightSum="5" > 

      <!-- table 1 --> 

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

       <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:ellipsize="end" 
        android:maxLines="1" 
        android:text="test1test2test3test4test5" 
        android:textSize="20dp"/> 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 


       </TableLayout> 
      </TableLayout> 

      <!-- table 2 --> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:text="test" 
        android:textSize="20dp" /> 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

       </TableLayout> 
      </TableLayout> 

      <!-- table 3 --> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:text="test" 
        android:textSize="20dp" /> 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

       </TableLayout> 
      </TableLayout> 

      <!-- table 4 --> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:text="test" 
        android:textSize="20dp" /> 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

       </TableLayout> 
      </TableLayout> 

      <!-- table 5 --> 

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

       <TextView 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:gravity="center_horizontal" 
        android:text="test" 
        android:textSize="20dp" /> 

       <TableLayout 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 

       </TableLayout> 
      </TableLayout> 

     </LinearLayout> 

回答

5

对于外部TableLayouts,使用android:layout_width =“0dp”而不是android:layout_width =“wrap_content”。

另外指定android:layout_weight =“1”。

删除android:weightSum =“5”进行自动调整大小。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

    <!-- table 1 --> 

    <TableLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:ellipsize="end" 
     android:gravity="center_horizontal" 
     android:text="test1 test2 test3 test4 test5" 
     android:textSize="20dp" /> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
    </TableLayout> 

<!-- table 2 --> 

    <TableLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="test" 
     android:textSize="20dp" /> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
    </TableLayout> 

<!-- table 3 --> 

    <TableLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="test" 
     android:textSize="20dp" /> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
    </TableLayout> 

<!-- table 4 --> 

    <TableLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="test" 
     android:textSize="20dp" /> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
</TableLayout> 

<!-- table 5 --> 

    <TableLayout 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center_horizontal" 
     android:text="test" 
     android:textSize="20dp" /> 

    <TableLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" > 
    </TableLayout> 
    </TableLayout> 

</LinearLayout> 

为什么你在TableLayout里面有空? 如果你想看到整个文本,然后设置机器人:ellipsize =“无”

+0

非常感谢您的答案!它的工作原理。我不知道0DP,然后设置重量,但它很好:)。关于TableLayout内部,我的未来工作中将充满其他视图。 –

0

设置你所有的表格布局的layout_height和layout_width为“FILL_PARENT”。 你会完成的。

0

您可以使用表行中的一个表的布局和使用weight_sum表格布局

 ` <TableLayout 
         android:layout_width="wrap_content" 
         android:layout_height="match_parent" 
         android:weightSum="5"/> 
      <TableRow 
         your text 
      /> 
      <TableRow 
         your text 
      /> 
     <TableRow 
         your text 
      /> 
     <TableRow 
         your text 
      /> 
     <TableRow 
         your text 
      /> 
     </TableLayout>` 
3

android:layout_width= "0dp"所有五个父表格布局而不是“wrap_content"

+1

感谢您的答案!有用 ;) –

相关问题