0

如何以编程方式将多个相对布局添加到线性布局。我试过了,但它给了我这个异常“指定的孩子已经有一个父母。你必须调用 removeview()”这是我的代码,

  RelativeLayout addlangmid = new RelativeLayout(mActivity);
                addlangmid.setBackgroundResource(R.drawable.language_bg_top);
                TextView langname = new TextView(mActivity);
                LayoutParams params = new LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(20, 0, 30, 30);
                langname.setText(str[0]);
                addlangmid.addView(langname, params);

                fulllay.addView(addlangmid, 0);


                RelativeLayout addlangmid1 = new RelativeLayout(mActivity);
                addlangmid.setBackgroundResource(R.drawable.language_bg_middle);
                TextView langname1 = new TextView(mActivity);
                LayoutParams params1 = new LayoutParams(
                        RelativeLayout.LayoutParams.WRAP_CONTENT,
                        RelativeLayout.LayoutParams.WRAP_CONTENT);
                params1.setMargins(20, 0, 30, 30);
                langname1.setText(str[0]);
                addlangmid1.addView(langname, params1);

                fulllay.addView(addlangmid1, 1);
4

1 回答 1

2

更改以下行

addlangmid1.addView(langname, params1);

addlangmid1.addView(langname1, params1);

您正在将 langname 添加到 addlangmid 和 addlangmid1。因此错误来了。

于 2013-04-10T10:19:43.280 回答