请检查一下。
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);
在这里,我必须在项目imageview的上面设置item3 Imageview。但我无法做到这一点。 在此代码中,item3是重叠项。 我必须将它设置在项目imageview的顶部。 请建议我哪里出错了。
提前致谢 高拉夫古普塔
答案 0 :(得分:0)
当您在最后添加项目时,它是相对布局,其中项目按照参数重叠
试试这个
将item3添加为第一个元素
和
在项目参数中添加此params.addRule(RelativeLayout.BELOW,item3.getId());