Search code examples
androidandroid-linearlayoutfill-parent

Android: height of LinearLayout didn't fill parent of ListView item


I have a ListView, the item is a RelativeLayout, at left is text, and the right side is ImageView in a LinearLayout.
I expect the height of the LinearLayout**(white background)** to be fillParent but it didn't.
The effect is :
enter image description here
Anyone can help on this?

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_list_item"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/main_list_item_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/main_list_item_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/main_list_item_more"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_list_item_delete_layout"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="#ffffff"
        android:gravity="center"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/main_list_item_delete"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/main_list_item_check_margin_lr"
            android:layout_marginRight="@dimen/main_list_item_check_margin_lr"
            android:contentDescription="@string/none"
            android:gravity="center"
            android:src="@drawable/ic_ctx_del1" />
    </LinearLayout>

</RelativeLayout>




The main.xml is as below which contains the ListView:

    <?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:gravity="fill"
    android:orientation="vertical" >

    <!-- content -->
    <LinearLayout
        android:id="@+id/center"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginLeft="@dimen/main_margin"
        android:layout_marginRight="@dimen/main_margin"
        android:orientation="vertical"
        android:layout_above="@id/adview_ayout">

        <ListView
            android:id="@+id/main_list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
        </ListView>

    </LinearLayout>
    <!-- content end -->

    <LinearLayout
        android:id="@+id/main_layout_pb"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@android:color/transparent"
        android:gravity="center"
        android:orientation="vertical"
        android:clickable="true"
        android:visibility="gone">

        <ProgressBar
            android:id="@+id/main_pb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical|center_horizontal" >
        </ProgressBar>
    </LinearLayout>

enter code here


Solution

  • I believe this is something that the parent RelativeLayout can't handle by itself - it creates kind of a vicious circle - parent defers the height to the child (wrap_content on the RelativeLayout) and the child defers the height to the parent (match_parent on main_list_item_delete_layout). What you can do is to anchor the top of the main_list_item_delete_layout to the parent and its bottom to another view that is guaranteed to be the bottom (the LinearLayout on the left, containing the three text views):

    <RelativeLayout ...>
        <LinearLayout 
            android:id="@+id/left_layout" 
            android:layout_toLeftOf="@+id/main_list_item_delete_layout"
            android:layout_toStartOf="@+id/main_list_item_delete_layout"
            ...
         >
         </LinearLayout>
    
        <LinearLayout
            android:id="@+id/main_list_item_delete_layout"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentTop="true"
            android:layout_alignBottom="@id/left_layout"
            ...>
        </LinearLayout>
    </RelativeLayout>
    

    PS: in your example, in case the text on the left LinearLayout is too long, it will overflow and overlap the delete layout - hence the addition of the anchor I added to the left layout. PS2: I know this solution is very customized to your layout. If you are set on using the RelativeLayout as a parent, you might want to try the new ConstraintLayout.