具有不同行高的ListView中的Android RelativeLayout fill_parent意外行为

Jam*_* A. 5 layout android listview relativelayout

我正在进行一个项目的小更新,我在列表视图中遇到了Relative_Layout和fill_parent的问题.我试图在每行的两个部分之间插入一个分隔符,就像默认拨号器的调用日志中的分隔符一样.我检查了Android源代码,看看他们是如何做到的,但是在复制他们的解决方案时遇到了问题.

首先,这是我的行项目布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout01"
 android:layout_width="fill_parent" 
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:padding="10dip" 
 android:layout_height="fill_parent" 
 android:maxHeight="64dip" 
 android:minHeight="?android:attr/listPreferredItemHeight">
 <ImageView android:id="@+id/infoimage" 
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" 
  android:clickable="true"
  android:src="@drawable/info_icon_big" 
  android:layout_alignParentRight="true" 
  android:layout_centerVertical="true"/>
 <View android:id="@+id/divider" 
  android:background="@drawable/divider_vertical_dark" 
  android:layout_marginLeft="11dip" 
  android:layout_toLeftOf="@+id/infoimage" 
  android:layout_width="1px" 
  android:layout_height="fill_parent"
  android:layout_marginTop="5dip" 
  android:layout_marginBottom="5dip" 
  android:layout_marginRight="4dip"/>
 <TextView android:id="@+id/TextView01" 
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"
  android:layout_centerVertical="true" 
  android:layout_toRightOf="@+id/ImageView01" 
  android:layout_toLeftOf="@+id/divider"
  android:gravity="left|center_vertical" 
  android:layout_marginLeft="4dip" 
  android:layout_marginRight="4dip"/>
 <ImageView android:id="@+id/ImageView01" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:layout_alignParentLeft="true" 
  android:background="@drawable/bborder" 
  android:layout_centerVertical="true"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

我面临的问题是每行都有一个不同高度的缩略图(ImageView01).如果我将RelativeLayout的layout_height属性设置为fill_parent,则分隔符不会垂直缩放以填充行(它只是一个1px点).如果我将layout_height设置为"?android:attr/listPreferredItemHeight",则分隔符会填充该行,但缩略图会缩小.我已经在适配器的getView()方法中进行了一些调试,并且一旦行具有适当的高度,似乎没有正确设置分隔符的高度.

这是getView()方法的一部分:

public View getView(int position, View view, ViewGroup parent) {
            if (view == null) {
                view = inflater.inflate(R.layout.tag_list_item, parent, false);
            }
Run Code Online (Sandbox Code Playgroud)

该方法的其余部分只是为行设置适当的文本和图像.

另外,我在适配器的构造函数中创建了inflater对象: inflater = LayoutInflater.from(context);

我错过了必不可少的东西吗 或者fill_parent不适用于动态高度?

Wes*_*ton 6

如果有其他人遇到这个问题,我找到的唯一方法(除了固定高度)是切换到LinearLayout.我猜这是由于LinearLayouts的绘制方式与RelativeLayouts的根本区别.罗曼盖伊它在谈到这个文章.LinearLayouts调用onMeasure()一次为宽度,一次调用高度,而RelativeLayouts则不调,因此一旦其他所有布局完成后,他们无法返回并填充垂直分隔符到propper高度.解决方案如下:

<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>
Run Code Online (Sandbox Code Playgroud)

这应该可以获得所需的结果,尽管它不如仅仅是RelativeLayout那样有效.


dsa*_*ler 0

我怀疑你的布局中存在循环依赖。您确定要使行layout_height成为 吗fill_parent?听起来您希望该行与最大的子视图(@id/ImageView01在本例中)具有相同的高度,因此您希望layout_height="wrap_content"RelativeLayout. 这样,高度将从图像传播到行,然后传播到具有 的其他子项layout_height="fill_parent"