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