Android ListView行填充/边距

时间:2011-12-16 10:41:51

标签: android xml listview height divider

我的Android列表视图存在问题。我的列表使用自定义arrayadapter,它为消息图像设置onClickListener,列表中有一个onItemClickListener。一切正常,但正如您在下面的屏幕截图中看到的那样,图像不会填充它的父级。 因此,当我单击图像的上方,下方或右侧时,将调用onItemClickListener。

我希望将行分为两部分。我相信这个解决方案在于list.xml的row.xml或xml。如何用我的图像填充整行?

The Screen Shot

row.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:background="@drawable/listitem_background"
android:id="@+id/row2">
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/logominimini" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:singleLine="true"
        android:textColor="@color/darkgreen"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:id="@+id/bottomtext"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="@color/black"
    />
</LinearLayout>
     <TextView 
         android:id="@+id/divider"
         android:layout_width="3dp"
         android:layout_height="fill_parent"
         android:background="@drawable/divider_background"
         android:visibility="gone"/>
<ImageView
    android:id="@+id/selectmessage"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:visibility="visible"
    android:paddingRight="20dp"
    android:paddingLeft="40dp"
    android:background="@drawable/listitem_background"
    android:layout_marginLeft="0dip"
    android:layout_marginRight="0dip"
    android:layout_gravity="center_vertical"
    android:src="@drawable/messageunselected" />

   </LinearLayout>

列表视图:

   <ListView 
android:id="@+id/android:list"
android:fastScrollEnabled="true" 
android:listSelector="@drawable/listitem_background" 
android:layout_height="wrap_content" 
android:layout_width="fill_parent"></ListView>

2 个答案:

答案 0 :(得分:4)

删除评论的属性android:padding="6dip",将其放在ImageView或其他地方,但不放在父LinearLayout中:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
<!-- remove this: android:padding="6dip" -->
android:background="@drawable/listitem_background"
android:id="@+id/row2">
<ImageView
    android:id="@+id/icon"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_marginRight="6dip"
    android:src="@drawable/logominimini" />
<LinearLayout
    android:orientation="vertical"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/toptext"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:singleLine="true"
        android:textColor="@color/darkgreen"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1"
        android:id="@+id/bottomtext"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:textColor="@color/black"
    />
</LinearLayout>
     <TextView 
         android:id="@+id/divider"
         android:layout_width="3dp"
         android:layout_height="fill_parent"
         android:background="@drawable/divider_background"
         android:visibility="gone"/>
<ImageView
    android:id="@+id/selectmessage"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_centerVertical="true"
    android:visibility="visible"
    android:paddingRight="20dp"
    android:paddingLeft="40dp"
    android:background="@drawable/listitem_background"
    android:layout_marginLeft="0dip"
    android:layout_marginRight="0dip"
    android:layout_gravity="center_vertical"
    android:src="@drawable/messageunselected" />

   </LinearLayout>

答案 1 :(得分:0)

您想要填充整行的图像第一张图片或上一张图片

对于两个ImageViews你保持填充,那么那些可以填充父?

请检查您的代码一次,您的问题也不清楚。

我看到了你的截图,我以为你在谈论第二张图片。

如果是,则在ImageView中删除填充。

我认为它会对你有所帮助。