2010-04-29 22 views
5

我目前正在对项目进行小型更新,并且在列表视图中遇到了Relative_Layout和fill_parent的问题。我试图在每一行的两部分之间插入一个分隔符,就像默认拨号程序的通话记录中的分隔符。我查看了Android源代码,看看他们是如何做到的,但是我在复制他们的解决方案时遇到了一个问题。Android的RelativeLayout fill_parent意外的行为在不同的行高的ListView中

要启动,这是我行的产品布局:

<?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 = LayoutInflater.from(context);

我缺少的东西必不可少的?或者fill_parent只是不适用于动态高度?

回答

0

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

+0

感谢您的回答!但是,分隔线仍然不会垂直填充该行。我不确定这种行为是否是一个错误,或者是否有其他缺失的东西。任何其他想法? – 2010-05-04 06:03:29

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

如果别人运行到这个问题,我发现的唯一途径(除了固定的高度除外)切换到LinearLayout中。我猜这是由于LinearLayouts如何绘制与RelativeLayouts的根本区别。罗曼盖伊在this的文章中谈到它。 LinearLayouts调用onMeasure()一次的宽度,一次调用高度,其中RelativeLayouts不调用,所以一旦布置其他所有东西,它们就无法返回并填充垂直分隔符到propper高度。解决方案如下:

​​

这应该会得到您想要的结果,尽管它不如单独使用RelativeLayout那么高效。