Android的RelativeLayout的FILL_PARENT意外的行为在ListView具有不同的行高意外、不同、行为、RelativeLayout

2023-09-06 03:31:33 作者:贱人慢走

我目前工作的一个小更新,以一个项目,我有一个列表视图的问题Relative_Layout和FILL_PARENT。我试图插入两部分之间​​的分隔每行中,很像分频器在默认拨号器的呼叫记录。我检查了Android源$ C ​​$ C,看看他们是如何做到的,但我在复制他们的解决方案遇到了问题。

I'm currently working on a small update to a project and I'm having an issue with Relative_Layout and fill_parent in a list view. I'm trying to insert a divider between two sections in each row, much like the divider in the call log of the default dialer. I checked out the Android source code to see how they did it, but I encountered a problem when replicating their solution.

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

To start, here is my row item layout:

<?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为机器人:ATTR /列表preferredItemHeight,除法填充行,但缩略图缩小。我在适配器的getView()方法做了一些调试,似乎分频器的高度不被设置正确,一旦行有它应有的高度。

The issue I'm facing is that each row has a thumbnail of varying height (ImageView01). If I set the RelativeLayout's layout_height property to fill_parent, the divider does not scale vertically to fill the row (it just remains a 1px dot). If I set layout_height to "?android:attr/listPreferredItemHeight", the divider fills the row, but the thumbnails shrink. I've done some debugging in the getView() method of the adapter, and it seems that the divider's height is not being set properly once the row has it's proper height.

下面是getView()方法的一部分:

Here is a portion of the getView() method:

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

该方法的其余部分简单地设置该行适当的文字和图片。

The rest of the method simply sets the appropriate text and images for the row.

另外,我创建了适配器的构造与充气对象:充气= LayoutInflater.from(上下文);

Also, I create the inflater object in the adapter's constructor with: inflater = LayoutInflater.from(context);

我缺少的东西必不可少的?还是FILL_PARENT只是不能与动态的高度工作?

Am I missing something essential? Or does fill_parent just not work with dynamic heights?

推荐答案

如果别人运行到这个问题,我发现的唯一途径(不是固定的高度除外)切换到的LinearLayout。我猜这是由于在如何LinearLayouts绘制与RelativeLayouts一个根本的区别。关于它在这文章。 LinearLayouts调用onMeasure()一次的宽度和一次高度,其中作为RelativeLayouts没有,所以他们没有办法回去,并填写一个垂直分隔线propper高度,一旦一切已经奠定了。解决方法如下:

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>

这应该得到你想要的结果,但它的效率不如单纯的RelativeLayout的。

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