2

I have a vertical LinearLayout which has a list view and one ImageView at the bottom and one at top and the list view filled up the space left behind.

It looks like this:

<LinearLayout layout_height="match_parent" layout_width="@dimen/width"
    orientation="vertical"> 
    <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/top">

     <ListView  android:layout_height="0px"
        android:background="#00ffff"
        android:layout_width="@dimen/width"
        android:layout_weight="1" />

     <ImageView layout_height="@dimen/width" layout_width="@dimen/width" id="@+id/bottom" layout_gravity="bottom|center_horizontal">

It works fine whenever I have the list view visible.

But whenever I set the ListView to visibility gone, the 'bottom' ImageView will popup to be just underneath the top Image View.

My question is why the bottom ImageView does not stay at the bottom despite I said ' android:layout_gravity="bottom|center_horizontal"' I have looked at hierarchyViewer, the height of the parent does match the screen's height. So the bottom Image view should honor the android:layout_gravity="bottom|center_horizontal" and stay at the bottom, right?

Krushna Chulet
  • 1,625
  • 1
  • 10
  • 9
michael
  • 106,540
  • 116
  • 246
  • 346
  • Do you require the `ListView` to be `gone`, or can you get away with setting it to `invisble`? I'm pretty sure the latter will give you what you're looking for. – MH. Apr 21 '12 at 03:12

2 Answers2

3

As alternative to my comments, you can replace your LinearLayout with a RelativeLayout. Agarwal already suggested this, but his code is a bit of a mess and at the time of this writing not correct in terms of what you want it to do.

Give below code a try. When you set the ListView to gone, the top and bottom images will stay positioned identical to when it's visible. Do note that I replaced the @dimens/width references, so you might want to put those back in.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">

    <ImageView android:id="@+id/top" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <ListView android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:layout_above="@+id/bottom"
        android:layout_below="@+id/top" android:background="#00ffff" />

    <ImageView android:id="@+id/bottom" android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
MH.
  • 45,303
  • 10
  • 103
  • 116
  • Thanks. Is it possible to make the ListView to 'grow' from bottom, instead from the top. i.e. the first element is displayed at the bottom the ListView and then second element is above the first on and son on and so forth? – michael Apr 23 '12 at 19:08
  • @michael: If I'm not mistaken you can use the `stackFromBottom` flag for this purpose. Either set it through xml: [`android:stackFromBottom`](http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:stackFromBottom), or code: [`setStackFromBottom(boolean)`](http://developer.android.com/reference/android/widget/AbsListView.html#setStackFromBottom%28boolean%29). *"When stack from bottom is set to true, the list fills its content starting from the bottom of the view."* – MH. Apr 23 '12 at 19:21
0

The best solution is to use ralaticelayout for this::::

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#0000FF">

 <ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/top" />
<ImageView android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/bottom" android:layout_alignParentBottom="true"/>
     <ListView  android:layout_height="fill_parent"
        android:background="#00ffff"
        android:layout_width="fill_parent"
        android:layout_below:@id/top android:layout_above:@id/bottom />


</RelativeLayout>
Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • 1
    I'm sorry, but your suggestion makes no sense at all. A `RelativeLayout` with `orientation` attribute? And a child view with weight? This will actually always render the `ListView` with a height of `0` - in other words it won't be visible at all. Finally, the `ListView` and bottom `ImageView` will overlay, which is probably not what the OP is after... – MH. Apr 21 '12 at 03:15
  • removed orientation and it is not need for relativelayout. and 0dp indicates that it occupies remaining space. – Shankar Agarwal Apr 21 '12 at 03:23
  • Have you actually tried your suggestion? As far as I know weights are ignored in a `RelativeLayout`, meaning it'll simply have a fixed height of zero... – MH. Apr 21 '12 at 03:26
  • ya right we just remove weight. sorry for that. removed it from post – Shankar Agarwal Apr 21 '12 at 03:27