如何动态地在某个元素下面添加相对布局

时间:2015-03-03 01:20:33

标签: java android xml parameters relativelayout

请帮我在RelativeLayout动态下添加RadioButton(我必须在课堂上创建RelativeLayoutRadioButton。)

这样的事情:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <RadioButton
          android:id="@+id/radioButton3"
          android:text="RadioButton" />


    <RelativeLayout
          android:id="@+id/relativeLayout2"
          android:layout_below="@+id/radioButton3"
          android:layout_width="match_parent"
          android:layout_height="wrap_content">

    </RelativeLayout>

</LinearLayout>

要查看我可以写:

myLayout.addView(myView, layoutParams);

但ViewGroup怎么样?

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解你。请检查并给我反馈。谢谢!

    LinearLayout mLayout = (LinearLayout) findViewById(R.id.myLayout);
    LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    RadioButton mRadioButton = new RadioButton(this);
    mRadioButton.setLayoutParams(p);
    mLayout.addView(mRadioButton);

    p = new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    RelativeLayout mRelativeLayout = new RelativeLayout(this);
    mRelativeLayout.setLayoutParams(p);
    mLayout.addView(mRelativeLayout);