0

我正在尝试使用布局参数将图像视图放置在另一个图像视图下方,但它不起作用..我想做到这一点: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);
                    }

怎么了 ?

4

1 回答 1

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

希望能帮助到你

于 2012-08-24T02:10:46.560 回答