2017-01-28 38 views
0

總部設在這個問題How to set RelativeLayout layout params in code not in xml我做了這樣的代碼:如何設置的LinearLayout的LayoutParams顯示所有高度

LinearLayout oLl = new LinearLayout(getContext()); 

    TextView oNum = new TextView(getContext()); 
    oNum.setText(String.valueOf(nPos + 1) + ". "); 
    oNum.setTypeface(null, Typeface.BOLD); 
    oNum.setTextColor(Color.RED); 
    oNum.setTextSize(18); 
    oNum.setPadding(Dp_to_Px(10),Dp_to_Px(15),0,0); 

    TextView oText = new TextView(getContext()); 
    oText.setText(oPreg.getcPregunta()); 
    oText.setTypeface(null, Typeface.BOLD); 
    oText.setTextColor(Color.BLACK); 
    oText.setTextSize(18); 

    LinearLayout.LayoutParams oLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT); 

    oLl.addView(oNum); 
    oLl.addView(oText,oLp); 

    oContainer.addView(oLl,oLp); 

但它減少的最後一行。

enter image description here

編輯:

單選按鈕的代碼:

RadioGroup oRg = new RadioGroup(getContext()); 
    oRg.setOrientation(RadioGroup.HORIZONTAL); 
    oRg.setPadding(Dp_to_Px(10),Dp_to_Px(6),0,0); 

    RadioButton oRbSi = new RadioButton(getContext()); 
    oRbSi.setText(getContext().getString(R.string.Si)); 
    oRbSi.setPadding(0,0,Dp_to_Px(25),Dp_to_Px(5)); 

    RadioButton oRbNo = new RadioButton(getContext()); 
    oRbNo.setText(getContext().getString(R.string.No)); 

    oRg.addView(oRbSi); 
    oRg.addView(oRbNo); 

    oContainer.addView(oRg); 

而且oContainer定義:

<LinearLayout 
    android:id="@+id/encuesta_frg_contaier" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

</LinearLayout> 
+0

對不起,如果我錯了,但我們不能在xml中設置包裝內容高度爲線性佈局? – Nobody

+0

@Nobody是的,但我需要在應用程序的這一點動態創建控件。 –

回答

1

正是因爲這一點:oNum.setPadding(Dp_to_Px(10),Dp_to_Px(15),0,0);

oNum TextView頂部有填充符,它將oText TextView推到 RadioGroup下面。嘗試在oLl上設置填充,而您的問題應該解決。

相關問題