以编程方式将imageview放在imageview下面

时间:2012-08-23 18:43:56

标签: android parameters position imageview

我正在尝试使用布局参数将imageview放在另一个下方,但它无法正常工作.. 我想完成这个:http://imgur.com/eZfEI

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

                    iv_album[i] = new ImageView(main.this);
                    iv_album[i].setImageBitmap(resized_a_p);
                    iv_album[i].setPadding(0, 5, 5, 0);
                    iv_album[i].setId(i);
                    iv_album[i].setOnLongClickListener(main.this);
                    iv_album[i].setOnClickListener(main.this);
                    if (i>1){
                        params.addRule(RelativeLayout.BELOW, iv_album[i].getId() - 1);
                    }

出了什么问题?

1 个答案:

答案 0 :(得分:1)

为每个ImageView创建LayoutParams对我有用:

RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.layoutConnection);

ImageView[] iv_album = new ImageView[8];
for (int i = 0; i < 8; i++) {

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);

    iv_album[i] = new ImageView(this);
    iv_album[i].setImageDrawable(this.getResources().getDrawable(R.drawable.ic_launcher));
    iv_album[i].setPadding(0, 5, 5, 0);
    iv_album[i].setId(i + 1); // BELOW id 0 doesn't work
    if (i > 0) {
        params.addRule(RelativeLayout.BELOW, iv_album[i - 1].getId());
    }

    layout.addView(iv_album[i], params);
}

希望有所帮助