列表项目的背景是伸展超过高度?

时间:2013-03-04 11:54:02

标签: android android-listview custom-adapter android-drawable

我有一个带有自定义适配器(磁贴)设计的列表视图。一切都应该工作正常,但事实是,尽管我给layout_height一个固定的值,til(listview项目背景)延伸得太多了。这是代码:

这是自定义磁贴的xml。注意85dp的固定高度。背景可绘制不应超出此高度?

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="85dp"
    android:background="@drawable/tile_job" >

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="9dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="13dp"
        android:text="Title" />

    <TextView
        android:id="@+id/pickup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="38dp"
        android:layout_alignLeft="@+id/title"
        android:textSize="11dp"
        android:text="Pickup Address" />

    <TextView
        android:id="@+id/destination"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/pickup"
        android:layout_below="@+id/pickup"
        android:layout_marginTop="6dp"
        android:textSize="11dp"
        android:text="Destination Address" />

    <TextView
        android:id="@+id/time"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/destination"
        android:layout_alignParentRight="true"
        android:layout_marginRight="40dp"
        android:text="Time"
        android:textSize="11dp" />

    <TextView
        android:id="@+id/date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/time"
        android:layout_below="@+id/time"
        android:text="Date"
        android:layout_marginTop="7dp"
        android:textSize="11dp" />

</RelativeLayout>

现在我在一个简单的列表视图中使用它:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/app_bg" >

    <RelativeLayout
        android:id="@+id/relativeLayout1"
        android:layout_width="wrap_content"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/strip_top" >

        <TextView
            android:id="@+id/strip_text"
            style="@style/TopStripText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Locate" />
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="280dp"
        android:layout_height="440dp"
        android:layout_below="@+id/relativeLayout1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dp"
        android:background="@drawable/bg_booking" >

        <ListView
            android:id="@+id/activebookings"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" >
        </ListView>
    </RelativeLayout>

是什么导致瓷砖垂直过度拉伸?

===========

修改 这是请求的代码:

布局通胀:

public class JobAdapter extends BaseAdapter
{
    private ArrayList<Job> bookingArrayList;
    private LayoutInflater mInflater;

    public JobAdapter(Context context, ArrayList<Job> results)
    {
        bookingArrayList = results;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount()
    {
        return bookingArrayList.size();
    }

    @Override
    public Object getItem(int arg0)
    {
        return bookingArrayList.get(arg0);
    }

    @Override
    public long getItemId(int position)
    {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder;
        if (convertView == null)
        {
            convertView = mInflater.inflate(R.layout.tile_job, null);
            holder = new ViewHolder();
            holder.date = (TextView) convertView.findViewById(R.id.date);
            holder.time = (TextView) convertView.findViewById(R.id.time);
            holder.pickup = (TextView) convertView.findViewById(R.id.pickup);
            holder.destination = (TextView) convertView.findViewById(R.id.destination);
            holder.title = (TextView) convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.date.setText(bookingArrayList.get(position).getDate());
        holder.time.setText(bookingArrayList.get(position).getTime());
        holder.pickup.setText(bookingArrayList.get(position).getPickup());
        holder.destination.setText(bookingArrayList.get(position).getDestination());
        holder.title.setText(bookingArrayList.get(position).getTitle());

        return convertView;
    }

    static class ViewHolder
    {
        TextView date;
        TextView time;
        TextView pickup;
        TextView destination;
        TextView title;
    }
}

1 个答案:

答案 0 :(得分:1)

您创建的行如下:

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

现在试试这个:

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

如果未附加父布局,则将忽略xml中的布局参数。您设置为false,因为您没有立即将视图附加到根(这在返回视图时会自动发生)。您可以阅读有关此问题的更多信息here