动态地向线性布局添加多个相对布局

时间:2013-04-10 10:17:19

标签: android android-layout android-linearlayout android-view

如何以编程方式向线性布局添加多个相对布局。 我试过但是它给了我这个例外“指定的孩子已经有了父母。你必须调用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);

1 个答案:

答案 0 :(得分:2)

更改以下行

addlangmid1.addView(langname, params1);

addlangmid1.addView(langname1, params1);

您正在为addlangmid和addlangmid1添加langname。因此错误即将来临。