Reputation: 16798
I have a LinearLayout with multiple children and I want the ListView to fill the remaining bottom half of the parent layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
>
<AutoCompleteTextView
android:id="@+id/from_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:hint="@string/hint_from"
android:singleLine="true"
android:imeOptions="actionNext"
/>
<AutoCompleteTextView
android:id="@+id/to_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="@string/hint_to"
android:singleLine="true"
android:imeOptions="actionSearch"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/inverse_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".4"
android:layout_margin="10dp"
android:text="@string/inverse"
android:onClick="onClick"
/>
<Button
android:id="@+id/search_btn"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".6"
android:layout_margin="10dp"
android:text="@string/search"
android:onClick="onClick"
/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="@color/holo_blue_dark"
/>
<ListView
android:id="@+id/favorites_lv"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight=".5"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:layout_gravity="bottom"
android:background="#700"
/>
</LinearLayout>
But I'm getting this result:
Also, as you can see, there is a problem with ListView items height. The item layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_menu_mylocation"
android:contentDescription="@string/favorites_icon_cd"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="20dp"
android:orientation="vertical"
android:gravity="left|center_vertical"
>
<TextView
android:id="@+id/from_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
/>
<TextView
android:id="@+id/to_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
/>
</LinearLayout>
</LinearLayout>
How to solve these problems?
Upvotes: 0
Views: 402
Reputation: 86958
I have a LinearLayout with multiple children and I want the ListView to fill the remaining bottom half of the parent layout:
You can try simply changing the ListView's height to match_parent
and not using layout_weight
:
<ListView
android:id="@+id/favorites_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
Also, as you can see, there is a problem with ListView items height.
To use your layout simply change the height of the nested LinearLayout to wrap_content
:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:orientation="vertical"
android:gravity="left|center_vertical"
>
(If you want the image centered you can add: android:layout_gravity="center_vertical"
to your ImageView.)
Though if you use one RelativeLayout rather than two LinearLayouts, it will be faster to Android to draw. Try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:src="@android:drawable/ic_menu_mylocation"
android:contentDescription="@string/favorites_icon_cd"
/>
<TextView
android:id="@+id/from_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@id/image"
android:ellipsize="end"
/>
<TextView
android:id="@+id/to_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@id/from_tv"
android:layout_toRightOf="@id/image"
android:ellipsize="end"
/>
</RelativeLayout>
Upvotes: 3