2012-06-09 72 views
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); 

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

在此先感谢 拉夫古普塔

回答

0

,你在最后加入的项目,它是相对的布局,其中项目重叠按PARAMS

试试这个

新增项目3作为第一要素

项PARAMS添加此params.addRule(RelativeLayout.BELOW,item3.getId( ));