【问题标题】:How to add relative Layout below some element dynamically如何在某些元素下方动态添加相对布局
【发布时间】:2015-03-03 01:20:32
【问题描述】:

请帮我在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 呢?

【问题讨论】:

  • 格式化代码并为RelativeLayout添加一个缺少的括号,以便xml有效

标签: java android xml parameters android-relativelayout


【解决方案1】:

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

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

【讨论】:

  • 谢谢,但它不能按我的需要工作(如果我需要 mLayout 的垂直方向,你的代码可以正常工作,但我需要水平,因为它必须包含多个水平视图(单选按钮,图片等)和下一行的RelativeLayout。
  • 为什么不在现有的线性布局中添加另一个线性布局,并将方向设置为水平?然后,您可以将任何内容放入新的线性布局中。看看是不是你想要的
猜你喜欢
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-06
  • 1970-01-01
  • 2013-03-28
  • 2013-12-31
相关资源
最近更新 更多