2015-03-03 59 views
1

請幫我在RadioButton下動態添加RelativeLayout(我必須在課程中創建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?

+0

格式代碼,並添加缺少的支架的RelativeLayout所以XML是有效的 – 2015-03-06 17:04:13

回答

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

謝謝,但我需要它這麼想的工作(如果我需要mLayout垂直方向的代碼工作正常,但我需要的水平,因爲它必須在下一行包含多於一個視圖水平(RadioButton,圖片等)和RelativeLayout – 2015-03-03 16:18:06

+0

爲什麼不在已有線性佈局中添加另一個線性佈局,並將方向設置爲水平?然後,您可以將任何內容新的線性佈局讓我們看看它是否是你想要的 – 2015-03-03 17:24:33