项目中,遇到这样问题,
网格布局宽度自适应,但是高度设不了,文字和图片下面有一大片空白,将高度写死为306dp也不可以,
多方查找,得到答案,需要在适配器中 设置layoutparams设置高度,
代码如下
public class ServiceGridBaseAdapter extends BaseAdapter{
private Context mContext;
private List<StaffEntity> mList;
private StaffEntity entity;
public ServiceGridBaseAdapter() {
super();
}
public ServiceGridBaseAdapter(Context mContext, List<StaffEntity> mList ) {
super();
this.mContext = mContext;
this.mList = mList;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return mList.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
entity = mList.get(position);
convertView = LayoutInflater.from(mContext).inflate(R.layout.item_staff_grid, null);
if(holder !=null){
holder = (ViewHolder) convertView.getTag();
} else {
holder = new ViewHolder();
holder.imageView = (ImageView)convertView.findViewById(R.id.item_service_photo);
holder.name = (TextView)convertView.findViewById(R.id.item_service_name);
holder.cell = (TextView)convertView.findViewById(R.id.item_service_cell);
AbsListView.LayoutParams params = new AbsListView.LayoutParams(
android.view.ViewGroup.LayoutParams.FILL_PARENT, 306
);
convertView.setLayoutParams(params);
convertView.setTag(holder);
}
if(entity!=null){
holder.name.setText(entity.getName());
holder.cell.setText(entity.getTel());
// holder.imageView.setImageResource(Drawable.createFromPath(entity.getPhoto()));
}
return convertView;
}
class ViewHolder{
TextView name,cell;
ImageView imageView;
}
}
原文地址:http://blog.csdn.net/kan1kan5/article/details/42914839