在ListView中调整单个ImageView的大小

时间:2014-09-20 16:05:16

标签: java android android-layout listview

我需要根据条件更改ImageView,问题是ImageView大小只能通过ImageView.getLayoutParameters()。width / height进行更改,这样做会改变ListView中每个项目的宽度。

有没有办法改变单个ImageVIew的大小?至于现在,我无法找到一种方法。

2 个答案:

答案 0 :(得分:1)

这是我的自定义适配器。我可以通过将params设置为imageView来重新调整图像大小。希望对你有帮助!

p.s最糟糕的是,通过这种方式,您每次都必须创建视图!它比使用viewHolder慢。

public class Adapter extends BaseAdapter {

ArrayList<String> list = new ArrayList<String>();
Activity activity;

public Adapter(Activity activity) {
    this.activity = activity;
    list.add("Item 1");
    list.add("Item 2");
    list.add("Item 3");
    list.add("Item 4");
    list.add("Item 5");
    list.add("Item 6");
}

@Override
public int getCount() {
    return list.size();
}

@Override
public Object getItem(int i) {
    return list.get(i);
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {


    LayoutInflater inflater = activity.getLayoutInflater();
    convertView = inflater.inflate(R.layout.adapter,null);

    ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
    TextView textView = (TextView) convertView.findViewById(R.id.textView);


    textView.setText(list.get(i));
    //Condition
    if (i == 5)
    {
        setLayoutParams(imageView,40,40);
    }
    else {
        setLayoutParams(imageView,70,70);
    }

    imageView.setImageDrawable(activity.getResources().getDrawable(R.drawable.ic_launcher));
    return convertView;
}



private View setLayoutParams(View view, int width, int height)
{
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width,height);
    view.setLayoutParams(params);
    return  view;
}
}

答案 1 :(得分:0)

你必须写if else条件

if(yourcondtion)
{
setlayoutparams();
}

else
{
setoriginallayoutparams();
}