简体   繁体   中英

Android: fill relative layout height (item layout) inside ListView

I'm not able to make a View that fill the height of a RelativeLayout , which is the layout of ListView items. Here is my layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="wrap_content" >

<View 
    android:id="@+id/fill_the_height_please"
    android:layout_width="15dp"
    android:layout_height="match_parent"
    android:background="@color/red"/>

<TextView
    android:id="@+id/delivery_list_item_dest"
    android:layout_width="wrap_content"
    android:layout_toRightOf="@id/fill_the_height_please"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="2dp"
    android:text="Destinatario" />

<TextView
    android:id="@+id/delivery_list_item_address"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@id/delivery_list_item_dest"
    android:layout_below="@id/delivery_list_item_dest"
    android:layout_marginTop="5dp"
    android:layout_marginRight="5dp"
    android:text="Location" />

<ImageButton
    android:id="@+id/delivery_list_item_mapBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="0dp"
    android:src="@drawable/map_location"
    android:background="@null" />

View with id fill_the_height_please not fill the item height. In figure below you can see the result (nothing is shown):

在此处输入图片说明

I'd like something like this:

在此处输入图片说明 ù

I have also try to change view layout as follows but not works anyway:

<View 
    android:id="@+id/fill_the_height_please"
    android:layout_width="15dp"
    android:layout_height="match_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentBottom="true"
    android:background="@color/red"/>

Please can you help to achieve my goal? Thank you.

一种建议是将layout_alignTop =“ @ + id / delivery_list_item_mapBtn”和layout_alignBottom =“ @ + id / delivery_list_item_mapBtn”添加到视图中并使其始终适合图像高度。

Try changing

android:layout_toRightOf="@id/delivery_list_item_statusBtn"

to

android:layout_toRightOf="@id/fill_the_height_please"

You don't have delivery_list_item_statusBtn. But i see red part even without changing this. Try it and if it doesn't help please post code of your adapter.

Looking here it seems RelativeLayout has a bug in version 17 and lower. So i have change my layout as follows:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<View 
    android:id="@+id/fill_the_height_please"
    android:layout_width="15dp"
    android:layout_height="match_parent"
    android:background="@color/red"/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:orientation="vertical" >
        <TextView
            android:id="@+id/delivery_list_item_dest"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Destinatario"
            android:layout_marginBottom="5dp" />

        <TextView
            android:id="@+id/delivery_list_item_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Location" />
    </LinearLayout>

    <ImageButton
        android:id="@+id/delivery_list_item_mapBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="0dp"
        android:src="@drawable/map_location"
        android:background="@null" />

</RelativeLayout>

A little bit more complex but works. However i leave the question unresolved in case someone find a solution using only a RelativeLayout

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM