0

请检查这个。

    RelativeLayout layout = new RelativeLayout(this);

    ImageView item = new ImageView(this);
    item.setImageResource( R.drawable.invite );
    item.setAdjustViewBounds(true);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_LEFT, RelativeLayout.TRUE);
    item.setLayoutParams(params);
    item.setId( mIconIdCounter );
    layout.addView(item, params);
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, item.getId());

    TextView tv1=new TextView(getApplicationContext());
    tv1.setText("Invite");
    tv1.setTextSize(15);
    tv1.setId(2);
    params.addRule(RelativeLayout.BELOW, item.getId());
    layout.addView(tv1, params);

    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.RIGHT_OF, item.getId());

    ImageView item2 = new ImageView(this);
    item2.setImageResource( R.drawable.logout );
    item2.setAdjustViewBounds(true);
    item2.setLayoutParams(params);
    item2.setId(3);
    layout.addView(item2, params);

    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.BELOW, item2.getId());
    params.addRule(RelativeLayout.RIGHT_OF, item.getId());
    TextView tv2=new TextView(getApplicationContext());
    tv2.setText("Logout");
    tv2.setTextSize(15);
    tv2.setId(4);
    params.addRule(RelativeLayout.BELOW, item2.getId());
    layout.addView(tv2, params);


    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    ImageView item3 = new ImageView(this);
    item3.setImageResource( R.drawable.fbplaceholder);
    item3.setAdjustViewBounds(true);
    item3.setLayoutParams(params);
    item3.setId(5);
    layout.addView(item3, params);

在这里,我必须在 item imageview 的上面设置 item3 Imageview。但是我做不到。在此代码中 item3 是重叠项。我必须将它设置在项目 imageview 的顶部。请建议我哪里出错了。

在此先感谢 Gaurav Gupta

4

1 回答 1

0

当您在最后添加项目时,它是相对布局,其中项目根据参数重叠

尝试这个

添加 item3 作为第一个元素

在项目参数中添加此 params.addRule(RelativeLayout.BELOW, item3.getId());

于 2012-06-09T17:57:29.847 回答