I
I
IDOL12342013-11-08 03:46:54
Android
IDOL1234, 2013-11-08 03:46:54

Strange behavior of a ListView inside a LinearLayout?

Problem: If any item other than another ListView is below the original one, the top ListView shrinks it despite setting the Weight parameter.
the code

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1" >

    </ListView>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="5"
        android:scaleType="fitCenter"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

The strangest thing is that the ListView still reacts to this parameter, but very slightly. On StackOverflow, I could only find a mention that putting a ListView inside a LinearLayout is a VERY bad idea, and no details.
How can components be placed to get rid of this weird behavior? How to limit the height of the list if it can't be placed in a LinearLayout and similar containers?..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
P
palmut, 2013-11-08
@IDOL1234

Set the ListView android:layout_height="0dp"and it will have the expected option. In general, when using android:layout_weightthis control, it is desirable to set android:layout_height(or respectively android:layout_width) to 0dp. And the behavior will be more predictable, and it works faster. And the lower ImageView android:layout_weightis probably not needed at all.

D
Denis Zagaevsky, 2013-11-08
@zagayevskiy

In general, it seems to me android:layout_height="wrap_content"that ListView is a bad idea. Try making match_parent.

U
uaSaint, 2014-01-15
@uaSaint

Instead of LinearLayout, use RelativeLayout and then specify in ImageViewandroid:layout_below="@+id/listView1"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question