简体   繁体   中英

How to add relative Layout below some element dynamically

Please help me to add a RelativeLayout below a RadioButton dynamically (I have to create RelativeLayout and RadioButton in class).

Something like this:

<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>

To View I can write:

myLayout.addView(myView, layoutParams);

But how about ViewGroup?

I am not sure whether I understand u. Please check it and give me feedback. Thank you!

    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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM