2011-06-05 83 views
1

这里是我的布局XML:ViewFlipper宽度包裹内容和FILL_PARENT不工作

<?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"> 
    <TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:stretchColumns="true"> 
    <TableRow 
     android:background="#FF0000"> 
     <ViewFlipper 
     android:id="@+id/viewer_something" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
     <LinearLayout 
      android:id="@+id/view1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#CCCCCC"> 
      <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button one" /> 
     </LinearLayout> 
     <LinearLayout 
      android:id="@+id/view2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:background="#FFFFFF"> 
      <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="button two" /> 
     </LinearLayout> 
     </ViewFlipper> 
     </TableRow> 
     <TableRow 
      android:background="#FF0000"> 
      <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="button three" /> 
     </TableRow> 
     </TableLayout> 
    </LinearLayout> 

因此,在短期,我有一个ViewFlipper通过tablerow内。即使所有元素都指定了fill_parent,ViewFlipper也会缩小到其内容的大小。在下面的屏幕截图中,ViewFlipper具有灰色背景,TableRows具有红色背景。

http://imgur.com/FPnsE

回答

2

设置android:layout_weight="1"您ViewFlipper会工作。

+0

是的,确实如此。我不明白它为什么有效,但它工作。你可以解释吗? – 2011-06-05 23:34:19

+1

假设你有一个包含各种子视图的父视图。如果您将每个子视图设置为wrap_content,然后指定布局权重,则每个视图将展开填充其重量与总重量的比例的剩余空白区域。因此,如果在父视图(LinearLayout)内只有一个视图(ViewFlipper),则设置布局权重将使其填充剩余的空白区域。这就是布局重量的原因。至于为什么fill_parent的宽度不起作用,我不太确定,但这可能是测量尚未绘制视图的问题。 – 2011-06-05 23:39:32

+0

谢谢。仍然困惑为什么fill_parent没有在第一个地方工作 - 就像你说的 - 但我现在明白为什么添加layout_weight添加额外的保险,它会填补所有内容。 – 2011-06-06 00:10:33