0

我有一个带有自定义适配器(平铺)设计的列表视图。一切都应该正常工作,但问题是尽管我给 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;
    }
}
4

1 回答 1

1

您使用以下内容创建行:

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

现在试试这个:

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

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

于 2013-03-04T12:53:10.760 回答