Android RelativeLayout fill_parent unexpected behavior in a ListView with varying row heights

会有一股神秘感。 提交于 2019-11-30 21:04:32

If anybody else runs into this issue, the only way that I found (other than fixed height) is to switch to a LinearLayout. I'm guessing this is due to a fundamental difference in how LinearLayouts are drawn vs. RelativeLayouts. Romain Guy talks about it in this article. LinearLayouts call onMeasure() once for width and once for height, where as RelativeLayouts do not, so they have no way of going back and filling in a vertical divider to the propper height once everything else has been laid out. The solution follows:

<LinearLayout xmlns:...
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    >
    <RelativeLayout android:id="@+id/leftContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
    </RelativeLayout>
    <View android:id="@+id/divider"
        android:layout_width="1px"
        android:layout_height="fill_parent"
        android:background="@drawable/divider" />
    <RelativeLayout android:id="@+id/rightContent"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        >
    </RelativeLayout>
</LinearLayout>

This should get you the desired result, although it's not as efficient as solely a RelativeLayout.

I suspect a circular dependency in your layout. Are you sure you want the row's layout_height to be fill_parent? It sounds like you'd like the row to be the same height as the largest subview (@id/ImageView01 in this case), so you want layout_height="wrap_content" in the RelativeLayout. This way, the height will propagate from the image to the row and then down to the other children that have layout_height="fill_parent".

For anyone who is curious. It seems like this may be (or have been) a bug in the SDK. To fix the problem, I had to go with fixed row heights. This is the final layout code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="100dip" android:layout_width="fill_parent">
    <RelativeLayout android:id="@+id/Thumbnail"
        android:layout_alignParentLeft="true" android:paddingLeft="10dip"
        android:paddingTop="10dip" android:paddingRight="15dip"
        android:paddingBottom="10dip" android:layout_height="fill_parent"
        android:layout_width="wrap_content">
        <ImageView android:id="@+id/image"
            android:layout_width="80dip" android:background="@drawable/bborder"
            android:layout_centerVertical="true" android:adjustViewBounds="true"
            android:layout_height="80dp" android:cropToPadding="true" />
    </RelativeLayout>
    <View android:id="@+id/divider" android:background="@drawable/divider_light"
        android:layout_toRightOf="@+id/Thumbnail" android:layout_marginTop="10dip"
        android:layout_marginBottom="10dip" android:layout_width="1px"
        android:layout_height="fill_parent" android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true" />
    <LinearLayout 
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:paddingTop="10dp" android:paddingBottom="10dp" 
        android:layout_centerVertical="true" android:orientation="vertical"
        android:layout_marginLeft="20dip" android:layout_marginRight="10dip"
        android:layout_toRightOf="@+id/divider">
        <TextView android:id="@+id/title" android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true"
            android:ellipsize="end" />
        <TextView android:id="@+id/science_name" android:textAppearance="?android:attr/textAppearanceMedium"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:gravity="left|center_vertical" android:maxLines="1" android:singleLine="true"
            android:ellipsize="end" android:textStyle="italic" />
    </LinearLayout>
</RelativeLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!