android GridView网格布局,高度问题

 项目中,遇到这样问题,

网格布局宽度自适应,但是高度设不了,文字和图片下面有一大片空白,将高度写死为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;
}



}

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。