在列表视图中使用时,wrap_content和match_parent之间的区别

时间:2014-05-01 11:36:20

标签: android android-layout android-listview

我有一个列表视图,textview由自定义适配器充气。我的问题是,如果我正在创建一个高度和宽度wrap_content以及match_parent的列表视图,那会有什么不同。

我在这里添加我的代码。

activity_main.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="fill_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/list"
        android:layout_width="wrap_content/match_parent"
        android:layout_height="wrap_content/match_parent" />

</LinearLayout>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:padding="10dip"
        android:textSize="16sp"
        android:textStyle="bold" >
</TextView>

MainActivity.java

public class MainActivity extends Activity
{
    public static final String[] titles = new String[]
    { "Facebook", "Twitter" };
    ListView listView;
    List<RowItem> rowItems;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        rowItems = new ArrayList<RowItem>();
        for (int i = 0; i < titles.length; i++)
        {
            RowItem item = new RowItem(titles[i]);
            rowItems.add(item);
        }

        listView = (ListView) findViewById(R.id.list);
        CustomListViewAdapter adapter = new CustomListViewAdapter(this,
                R.layout.list_item, rowItems);
        listView.setAdapter(adapter);

    }

}

RowItem.java

public class RowItem
{
    private String title;

    public RowItem(String title)
    {
        this.title = title;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String title)
    {
        this.title = title;
    }

    @Override
    public String toString()
    {
        return title;
    }
}

CustomListViewAdapter.java

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 {
        TextView txtTitle;
    }

    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.txtTitle = (TextView) convertView.findViewById(R.id.label);
            Log.i("ListView",holder.txtTitle.toString());
            convertView.setTag(holder);
        } else
            holder = (ViewHolder) convertView.getTag();

        holder.txtTitle.setText(rowItem.getTitle());

        return convertView;
    }
}

这里我有一行Log.i("ListView",holder.txtTitle.toString());,它显示的日志多于使用wrap_content充气的listview项目,而使用match_parent显示完全完美。

这是 LogCat

  • 列表视图的LogCat 宽度高度等于wrap_content

      

    05-01 16:59:49.817:I / ListView(545):android.widget.TextView@43e45020
      05-01 16:59:49.867:I / ListView(545):android.widget.TextView@43e46350
      05-01 16:59:49.897:I / ListView(545):android.widget.TextView@43e47ce0

  • 列表视图的LogCat 宽度高度等于match_parent

      

    05-01 16:58:49.037:I / ListView(501):android.widget.TextView@43e526c8
      05-01 16:58:49.088:I / ListView(501):android.widget.TextView@43e52f08

0 个答案:

没有答案