【问题标题】:Android RelativeLayout fill_parent unexpected behavior in a ListView with varying row heights具有不同行高的 ListView 中的 Android RelativeLayout fill_parent 意外行为
【发布时间】:2011-02-13 16:34:06
【问题描述】:

我目前正在对项目进行小幅更新,并且在列表视图中遇到了 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 不适用于动态高度?

【问题讨论】:

    标签: android listview layout android-relativelayout


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答!但是,分隔线仍然不会垂直填充行。我不确定这种行为是错误还是我还缺少其他东西。还有其他想法吗?
    【解决方案2】:

    对于任何好奇的人。看起来这可能是(或曾经是)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>
    

    【讨论】:

      【解决方案3】:

      如果其他人遇到此问题,我发现的唯一方法(固定高度除外)是切换到 LinearLayout。我猜这是由于线性布局与相对布局的绘制方式存在根本差异。 Romain Guy 在this 文章中谈到了它。 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 高效。

      【讨论】:

        猜你喜欢
        • 2012-06-06
        • 1970-01-01
        • 2013-11-12
        • 2011-06-14
        • 2013-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        相关资源
        最近更新 更多