ListView 的 Layout_width [英] Layout_width of a ListView

查看:28
本文介绍了ListView 的 Layout_width的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我遇到了一个 listView 的属性 layout_width 的问题.

Ok so I encountered an issue with the attribut layout_width of a listView.

在我的项目中,我的列表视图有一个不适合屏幕宽度的背景图像.所以我在 wrap_content 上设置了 layout_width,并且我将 listView 水平居中.

In my project I had a background image for my listview that didn't fit the width of the screen. So I set the layout_width on wrap_content, and I centered horizontally the listView.

这似乎是个坏主意,因为在适配器中调用 GetView 方法的次数是显示单元格数量的 3 倍以上.如果我在 fill_parent 上设置 layout_width 问题就解决了.我创建了一个显示问题的简单项目.

It seems that this was a bad idea, because in the adapter the GetView method was called more than 3 times the number of cell displayed. If I set the layout_width on fill_parent the problem is resolved. I created a simple project that shows the problem.

这是活动的xml:

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

item_list xml :

The item_list xml :

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="16sp" >
</TextView>

最后是活动:

package fr.example.android.listviewbug;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

public class ExampleListViewBugActivity extends ListActivity{
    public static int cpt;

    public static final String[] COUNTRIES = new String[] {
        "Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra",
        "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina",
        "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan",
        "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium",
        "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia",
        "Bosnia and Herzegovina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory",
        "British Virgin Islands", "Brunei", "Bulgaria", "Burkina Faso", "Burundi",
        "Cote d'Ivoire", "Cambodia", "Cameroon", "Canada", "Cape Verde",
        "Cayman Islands", "Central African Republic", "Chad", "Chile"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      cpt = 0;
      setListAdapter(new CustomArrayAdapter(this, R.layout.list_item, COUNTRIES));  
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.d("AdapterTest", "onDestroy()");
    }

    private class CustomArrayAdapter extends ArrayAdapter<String>{
        public CustomArrayAdapter(Context context, int textViewResourceId, String[] countries) {
            super(context, textViewResourceId, countries);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Log.d("AdapterTest", " cpt:"+ ++cpt);
            return super.getView(position, convertView, parent);
        }
}
}

所以我的解决方案是简单地设置fill_parent上的layout_width,并修改listview背景的图片,添加一些透明边框以适应屏幕尺寸.

So my solution is to simply set the layout_width on fill_parent, and modify the image of the listview background to add some transparent borders to fit the screen size.

但我想知道为什么这个问题会出现在列表视图上的 wrap_content 中,以及为什么它不在文档中,如果它是一个已知问题...

But I'd like to know why this issue appears with wrap_content on a listview, and why it's not in the doc if it's a known problem...

推荐答案

通过将宽度设置为wrap_content",您是在告诉 ListView 与其最宽的子级一样宽.因此,ListView 必须测量它的项目并获取它必须在适配器上调用 getView() 的项目.这可能会发生多次,具体取决于布局传递的次数、父布局的行为等.

By setting the width to "wrap_content" you are telling ListView to be as wide as the widest of its children. ListView must therefore measure its items and to get the items it has to call getView() on the Adapter. This may happen several times depending on the number of layout passes, the behavior of the parent layout, etc.

请记住,不能保证在适配器上调用 getView() 的次数,也不能保证调用发生的顺序.因此,您必须实现 getView() 以尽可能高效.最重要的是,这不是问题,这是在这种情况下的预期行为.

Remember there are no guarantees on the number of times getView() is invoked on the Adapter, nor in what orders the calls will happen. You must therefore implement getView() to be as efficient as possible. The bottom line is, this is not an issue, it's the expected behavior in this situation.

这篇关于ListView 的 Layout_width的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆