以编程方式对齐RelativeLayout中的项目?

时间:2011-11-30 15:58:11

标签: android layout

我正在尝试将项目放在RelativeLayout中,以便首先显示图像,右侧显示标题,以及图像区域下方的内容。这三个内容项目彼此重叠。我知道我的LayoutParams可能没有设置,但我不确定为什么或如何解决它。有帮助吗?感谢。

RelativeLayout rl = (RelativeLayout)findViewById(R.id.contentTable);
        for(int i=0;i<itemIndexes.size();i++)
        {
            RelativeLayout item = new RelativeLayout(this);
            item.setId(i);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(320, RelativeLayout.LayoutParams.WRAP_CONTENT);
            if(i>0)
            {
                lp.addRule(RelativeLayout.RIGHT_OF,i-1);
            }
            item.setLayoutParams(lp);
            ImageView iv = new ImageView(this);
                if(globals.myHitetItems.get(itemIndexes.get(i)).image!=-1)
                {
                        iv.setImageResource(globals.myHitetItems.get(itemIndexes.get(i)).image);
                    item.addView(iv);
                }
                else
                {
                    iv.setImageResource(R.drawable.noimage);
                    item.addView(iv);
                }
                RelativeLayout.LayoutParams text1lp =new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                text1lp.addRule(RelativeLayout.RIGHT_OF,iv.getId());
                TextView tv = new TextView(this);
                    tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).name);
                    tv.setTextSize(20);
                    tv.setPadding(3, 3, 3, 3);
                    tv.setLayoutParams(text1lp);
                item.addView(tv);

                RelativeLayout.LayoutParams text2lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
                text2lp.addRule(RelativeLayout.BELOW,iv.getId());
                tv = new TextView(this);
                    tv.setText(globals.myHitetItems.get(itemIndexes.get(i)).content);
                    tv.setTextSize(20);
                    tv.setPadding(3, 3, 3, 3);
                    tv.setLayoutParams(lp);
                item.addView(tv);
            rl.addView(item);
        }

1 个答案:

答案 0 :(得分:0)

调试看起来很痛苦。听起来似乎没有设置或接受相关规则。我建议简化为静态测试集,以验证可以相对于彼此设置一行视图(并确保其中一行被锚定到布局本身)。