【问题标题】:How to set LinearLayout LayoutParams to show all height如何设置 LinearLayout LayoutParams 以显示所有高度
【发布时间】:2017-01-28 21:12:15
【问题描述】:

基于这个问题How to set RelativeLayout layout params in code not in xml我已经制作了这个代码:

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

但它切断了最后一行。

编辑:

单选按钮的代码:

 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>

【问题讨论】:

  • 对不起,如果我错了,但我们不能在 xml 中将包装内容高度设置为线性布局吗?
  • @Nobody 是的,但我需要在应用程序的此时动态创建我的控件。

标签: android android-layout layout layoutparams


【解决方案1】:

正因为如此:oNum.setPadding(Dp_to_Px(10),Dp_to_Px(15),0,0);

oNum TextView 在顶部有填充,它将oText TextView 推到oRg RadioGroup 下方。尝试在oLl 上设置填充,您的问题应该会得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-03
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多