Android RelativeLayout fill_父对象在具有不同行高的ListView中的意外行为
我目前正在对一个项目进行一个小的更新,我对列表视图中的相对布局和填充父项有一个问题。我试图在每行的两个部分之间插入一个分隔符,就像默认拨号程序的呼叫日志中的分隔符一样。我查看了Android源代码,看看他们是如何做到的,但在复制他们的解决方案时遇到了一个问题 首先,以下是我的行项目布局:Android RelativeLayout fill_父对象在具有不同行高的ListView中的意外行为,android,listview,layout,android-relativelayout,Android,Listview,Layout,Android Relativelayout,我目前正在对一个项目进行一个小的更新,我对列表视图中的相对布局和填充父项有一个问题。我试图在每行的两个部分之间插入一个分隔符,就像默认拨号程序的呼叫日志中的分隔符一样。我查看了Android源代码,看看他们是如何做到的,但在复制他们的解决方案时遇到了一个问题 首先,以下是我的行项目布局: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/RelativeLayout01" andro
<?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>
该方法的其余部分只是为行设置适当的文本和图像
另外,我在适配器的构造函数中创建充气器对象,使用:inflater=LayoutInflater.from(context)代码>
我是不是错过了一些重要的东西?或者“填充父对象”不适用于动态高度?我怀疑布局中存在循环依赖关系。是否确实要将行的布局\u高度
设置为填充\u父项
?听起来您希望行的高度与最大的子视图的高度相同(@id/ImageView01
,在本例中),因此您希望在相对视图中使用布局\u height=“wrap\u content”
。这样,对于任何好奇的人来说,高度将从图像传播到行,然后向下传播到具有layout\u height=“fill\u parent”的其他子级。看起来这可能(或已经)是SDK中的一个bug。为了解决这个问题,我不得不选择固定的排高。这是最终布局代码:
<?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>
如果其他人遇到此问题,我发现的唯一方法(固定高度除外)是切换到线性布局。我猜这是因为线性布局与相对布局的绘制方式存在根本性差异。罗曼·盖伊在文章中谈到了这一点。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>
这应该会得到你想要的结果,尽管它不如单纯的相对论有效。谢谢你的回答!但是,分隔符仍然不会垂直填充行。我不确定这种行为是否是一个bug,或者我是否遗漏了其他东西。还有其他想法吗?
<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>