android listview 列宽,android - Layout_width of a ListView - Stack Overflow

作者探讨了在项目中使用ListView时,设置layout_width为wrap_content导致背景图片不适配和Adapter重复加载的问题。解决方法是将宽度设为fill_parent,并讨论了原因。新技术标签:ListView适配,wrap_content,fill_parent
摘要由CSDN通过智能技术生成

Ok so I encountered an issue with the attribut layout_width of a 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.

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.

Here is the xml of the activity :

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

android:id="@android:id/list"

android:layout_width="wrap_content"

android:layout_height="fill_parent"

/>

The item_list xml :

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:padding="10dp"

android:textSize="16sp" >

And finally the Activity :

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{

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);

}

}

}

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.

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...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值