删除ListView项目底部的多余空格

时间:2014-11-15 00:55:36

标签: android android-layout

我在ListView项目底部遇到了一些过多的空白问题。在http://i.stack.imgur.com/wpm7a.png

处标有红色箭头的白色/负空格的示例

这是我的布局XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.thereversecookbook.ResultsList" >

    <ListView
        android:id="@+id/results_list_lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" >
    </ListView>

</RelativeLayout>

我的列表项XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <!-- cooking time -->

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:clickable="false"
        android:longClickable="false"
        android:paddingBottom="50dp"
        android:textColor="#CC0033"
        android:textSize="16sp" />

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:longClickable="false"
        android:maxHeight="50dp"
        android:maxWidth="50dp"
        android:src="@drawable/ic_clock" />

    <TextView
        android:id="@+id/desc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/icon"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:longClickable="false"
        android:textColor="#3399FF"
        android:textSize="14sp" />

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="false"
        android:isIndicator="true"
        android:longClickable="false" 
        android:scaleX="0.5"
        android:scaleY="0.5"/>

</RelativeLayout>

和CustomListAdapter:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId, List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    /*private view holder class*/
    private class ViewHolder {
        ImageView imageView;
        TextView txtTitle;
        TextView txtCookingTime;
        RatingBar ratingBar;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        RowItem rowItem = getItem(position);

        LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
            holder.txtCookingTime = (TextView) convertView.findViewById(R.id.desc);
            holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
            holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
            holder.ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar1);
            convertView.setTag(holder);
        } else 
            holder = (ViewHolder) convertView.getTag();

        holder.txtCookingTime.setText(rowItem.getCookingTime());
        holder.txtTitle.setText(rowItem.getTitle());
        //holder.imageView.setImageResource(rowItem.getImageId());
        holder.ratingBar.setRating(rowItem.getRating());
        holder.ratingBar.setStepSize((float)0.5);

        return convertView;
    }
}

任何人都有任何想法可能导致这种情况?非常感谢!

1 个答案:

答案 0 :(得分:4)

convertView = mInflater.inflate(R.layout.list_item, null);

Bzzzt。不要使用inflate()方法。你想使用三参数版本,传入你的convertView(“膨胀的小部件最终将成为这个的孩子......”)和false(“...但不要立刻让他们成为孩子们“):

convertView = mInflater.inflate(R.layout.list_item, parent, false);

这在你的情况下尤其重要,因为RelativeLayout如果它是你的布局文件的根容器而你使用这个版本的inflate()方法会变得很奇怪

您可以在Dave Smith's "Layout Inflation as Intended" blog post中了解详情。