Android:wrap_content 不适用于 ListView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11295080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:48:34  来源:igfitidea点击:

Android: wrap_content is not working with ListView

androidlistview

提问by user1471575

I am working on android. I want my list view to wrap its content horizontally and not to fill all the width. The wrap_content properties is not working. What to do?

我在安卓上工作。我希望我的列表视图水平包装其内容而不是填充所有宽度。wrap_content 属性不起作用。该怎么办?

回答by Jaydipsinh Zala

In order achieve wrap_contentheight in ListView, we need to use CustomListViewthat extendsour native ListView.

为了实现wrap_content高度ListView,我们需要使用CustomListView的是extends我们的母语ListView

MyListView.java

MyListView.java

public class MyListView extends ListView {

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }

}

in your layoutuse custom view like this,

在您layout像这样使用自定义视图中,

layout.xml

布局文件

<com.yourpackagename.MyListView
        ...       
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ... />

回答by K_Anas

As Romain Guy(Google Engineer works on UI toolkit) Said in his post

正如Romain Guy(谷歌工程师从事 UI 工具包工作)在他的帖子中所说

By setting the width to wrap_contentyou 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.

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

So if you set the layout width or layout height of your ListView to wrap_contentthe ListView will try to measure every single view that is attached to it - which is definitely not what you want.

因此,如果您将 ListView 的布局宽度或布局高度设置wrap_content为 ListView 将尝试测量附加到它的每个视图 - 这绝对不是您想要的。

Keep in mind:avoid setting wrap_contentfor ListViews or GridViews at all times, for more details see this Google I/O videotalking about the world of listview

请记住:wrap_content始终避免设置ListViews 或 GridViews,有关更多详细信息,请参阅此Google I/O 视频,讨论列表视图的世界

回答by david

It can be implemented by using LinearLayout.

它可以通过使用 LinearLayout 来实现。

Create like this example:

像这个例子一样创建:

public class LinearLayoutAdapter   {
    LinearLayout linearLayout;
    ArrayList<String>  arrayList 
    private View rootView;
    private static LayoutInflater inflater;

    public ChoicesAdapter_(ArrayList<String>  arrayList ,LinearLayout linearLayout)
    {
        this.index =index;
        this.arrayList=arrayList;
        this.linearLayout=linearLayout;
        notifyDataSetChanged();
    }
    private void notifyDataSetChanged() {
        linearLayout.removeAllViews();
        for (int i =0;i<getCount();i++){
            linearLayout.addView(getView(i,null,null));
        }
    }
    public int getCount() {
        return  arrayList.size();
    }
    public Object getItem(int position) {
        return null;
    }
    public long getItemId(int position) {
        return 0;
    }
    public View getView(final int position, View convertView, ViewGroup parent) {
       rootView = inflater.inflate(R.layout.row_list, null);
       TextView  textView = (TextView)rootView.findViewById(R.id.text);    
       textView.setText(arrayList.get(position));
        return rootView;
    }
}

MainActivity example:

主活动示例:

public class MainActivity extends Activity    {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ArrayList<String>  arrayList = new  ArrayList<String>();
        arrayList.add("Accent");
        arrayList.add("Acclaim");
        arrayList.add("Accord");
        arrayList.add("Achieva");
        arrayList.add("Aerio");
        arrayList.add("Aerostar");
        LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
        linearLayoutAdapter= LinearLayoutAdapter(arrayList,linearLayout);
    }
}

XML example:

XML 示例:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:textColor="@color/dark_gray"
    android:background="@color/white"
    android:textSize="20dp"
    android:text=""  />

回答by Venky

I want my list view to wrap its content horizontally and not to fill all the width. 

In case of List View you can't give wrap_content, because if you give wrap_content for List View only first three items(rows)will be shown rest will be ignored.. So don't use wrap_content.. Always use fill_parentor match_parent for List View

在列表视图的情况下,你不能给wrap_content,因为如果你给列表视图的 wrap_content 只显示前三个项目(行),其余的将被忽略..所以不要使用 wrap_content.. 总是使用fill_parent或 match_parent列表显示

For efficient design on List View you can see this World of ListView

为了在 List View 上进行高效设计,您可以看到这个ListView World