I have a RelativeLayout that includes three elements: a TableLayout at the top that contains some controls (layout_alignParentTop = true), a LinearLayout at the bottom (layout_alignParentBottom = true) that contains a couple buttons and a ListView that stretches between them (using layout_below and layout_above the two elements). That works great, but under certain circumstances, the bottom LinearLayout开发者_JS百科 needs to go away. If I use setVisibility to set it to View.GONE or set its height to 0 using setLayoutParams(), the ListView is stubbornly refusing to resize to take up the newly freed space. I've tried calling requestLayout() on basically everything in the hierarchy, and nothing seems to work. If I fire up the hierarchy viewer, I can see that the view is gone/has layout_height of 0, but it still doesn't work.
Ironically, after the hierarchy viewer is done, the layout corrects itself. I'm thinking that having my users run hierarchy viewer is probably unacceptable, though.
EDIT: The middle layout is actually a RelativeLayout containing the ListView, which might matter, I'm going to try and eliminate it. I've posted a simplified version of my XML that gives an idea of what I'm looking at.
<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>
Is there a particular reason you're using RelativeLayout for the parent? This sounds like a case in which you should use a vertical LinearLayout containing the TableLayout, the ListView, and then the LinearLayout (in that order), where the ListView has layout_height="0dip" and layout_weight="1". When you set visibility on the bottom LinearLayout, the layout weight of the ListView should cause it to expand to fill the space.
精彩评论