2010-11-18 70 views
1

我有一个RelativeLayout,它包含三个元素:顶部的TableLayout包含一些控件(layout_alignParentTop = true),底部的LinearLayout(layout_alignParentBottom = true),包含一对按钮和在它们之间延伸的ListView(使用layout_below和layout_above这两个元素)。这很好,但在某些情况下,LinearLayout的底部需要消失。如果我使用setVisibility将其设置为View.GONE或使用setLayoutParams()将其高度设置为0,则ListView将顽固地拒绝调整大小以占用新释放的空间。我已经尝试调用requestLayout()基本上在层次结构中的所有内容,似乎没有任何工作。如果我启动层次结构查看器,则可以看到该视图已经消失/具有0的layout_height,但它仍然不起作用。具有讽刺意味的是,在层次结构查看器完成之后,布局会自行修正。不过,我认为让我的用户运行层次结构查看器可能是不可接受的。Android:设置视图高度为0

编辑:中间布局实际上是一个包含ListView的RelativeLayout,这可能很重要,我会尝试并消除它。我已经发布了我的XML的简化版本,可以提供我正在查看的内容。

<RelativeLayout 
    android:id="@+id/main_container" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <TableLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/top_table" 
     android:layout_alignParentTop="true" 
     android:stretchColumns="2" 
     android:shrinkColumns="1"> 
     <!-- removed for simplicity --> 
    </TableLayout> 
    <RelativeLayout 
     android:id="@+id/main_content" 
     android:descendantFocusability="afterDescendants" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_below="@id/title_table" 
     android:layout_above="@id/ad_toolbar"> 
     <!-- removed for simplicity --> 
     <ListView 
      android:id="@+id/android:list" 
      android:layout_alignParentTop="true" 
      android:descendantFocusability="afterDescendants" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
    </RelativeLayout> 

    <LinearLayout 
     android:id="@+id/bottom_layout" 
     android:orientation="vertical" 
     android:layout_alignParentBottom="true" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"> 
     <!-- removed for simplicity --> 
    </LinearLayout> 
</RelativeLayout> 
+0

你可以发布.xml的布局吗? – 2010-11-18 01:37:16

+0

实际的XML非常复杂,因为布局由几个由不同活动使用的布局文件组成,但是我已经发布了上面的简化版本。我意识到中间区域实际上是一个包含ListView的RelativeLayout,它可能会有所作为,我将尝试并消除它。 – jjb 2010-11-18 17:51:28

回答

1

是否有一个特定的原因,你使用RelativeLayout为父?这听起来像是应该使用包含TableLayout,ListView和LinearLayout(按此顺序)的垂直LinearLayout,其中ListView具有layout_height =“0dip”和layout_weight =“1”。当您在底部LinearLayout上设置可见性时,ListView的布局权重应该使其展开以填充空间。

+0

layout_weight是正确的解决方案。或者他还没有想到,或者这个问题比我们想象的更复杂?我虽然怀疑它。这就是为什么XML会很有用。比我们可以快速修复它:D – 2010-11-18 03:07:01

+0

它是一个RelativeLayout,因为包含顶部和底部元素的布局文件用于多个活动,其中一些使用了RelativeLayout来适当定位子元素。并非所有可能的布局都可以像​​LinearLayout一样轻松工作。 – jjb 2010-11-18 17:53:46

相关问题