5

我目前正在对项目进行小幅更新,并且在列表视图中遇到了 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>

我面临的问题是每一行都有一个不同高度的缩略图(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);
            }

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

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

我错过了一些重要的东西吗?还是 fill_parent 不适用于动态高度?

4

3 回答 3

6

如果其他人遇到此问题,我发现的唯一方法(固定高度除外)是切换到 LinearLayout。我猜这是由于线性布局与相对布局的绘制方式存在根本差异。Romain Guy 在这篇文章中谈到了它。LinearLayouts 调用 onMeasure() 一次用于宽度和一次用于高度,而 RelativeLayouts 不会,因此一旦其他所有内容都已布置好,它们就无法返回并填充垂直分隔线到适当的高度。解决方案如下:

<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>

这应该会为您提供所需的结果,尽管它不如单独的 RelativeLayout 有效。

于 2011-08-01T18:45:53.860 回答
0

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

于 2010-05-01T20:51:20.517 回答
0

对于任何好奇的人。看起来这可能是(或曾经是)SDK 中的一个错误。为了解决这个问题,我不得不使用固定的行高。这是最终的布局代码:

<?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>
于 2010-12-08T23:17:33.810 回答