【问题标题】:Dynamic UI - How to add 3 linearlayouts into a parent linearlayout动态 UI - 如何将 3 个线性布局添加到父线性布局中
【发布时间】:2015-01-12 21:37:40
【问题描述】:

我在创建动态 UI 时遇到了一些麻烦。 我可以让它与一个 LinearLayout 一起工作,但我不知道如何添加更多。

这就是我现在拥有的,它是一个带有 3 个按钮的 LinearLayout。

代码如下:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        maakLayout();
    }

    private void maakLayout() {
        LinearLayout linearLayout = new LinearLayout(this);
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        LayoutParams layoutParams = new LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        linearLayout.setLayoutParams(layoutParams);

        setContentView(linearLayout);

        for (int i = 0; i < 3; i++) {
            Button button = new Button(this);
            button.setText("Test");
            button.setTag(i);
            button.setBackgroundResource(R.drawable.border_red);



            LayoutParams layoutTextParams = new LayoutParams(
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);
            // LayoutParams layoutImageParams =
            // new LayoutParams(100,100);
            layoutTextParams.topMargin = 5;
            layoutTextParams.leftMargin = 5;
            layoutTextParams.rightMargin = 5;
            button.setLayoutParams(layoutTextParams);

            linearLayout.addView(button);
        }
    }

我需要的是以下内容: 其静态 XML 代码如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.test.MainActivity" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_marginRight="5dp"
        android:orientation="vertical" >

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="144dp"
        android:layout_marginRight="5dp"
        android:orientation="vertical" >

        <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/border_blue"
        android:text="Test" />

    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/border_red"
        android:text="Test" />

    </LinearLayout>

</LinearLayout>

一旦我知道如何添加其他线性布局,我想我就很好了,谢谢你的帮助。

【问题讨论】:

  • 抱歉,我没明白怎么回事?有什么问题?
  • 看看我需要在没有静态 XML 的情况下制作它的示例
  • 这是您要找的吗? stackoverflow.com/questions/5731487/…
  • 不是真的,我需要知道如何在没有 XML 文件的情况下将 3 个线性布局添加到父线性布局中。
  • 嗯...您可以摆脱所有 4 个 LinearLayout 并改用 single RelativeLayout,这有助于降低布局计数(以获得更好的性能)。

标签: android android-layout dynamic


【解决方案1】:

LinearLayout 本身是View 的子类,因此您可以将其作为子类添加到其他LinearLayouts

像这样:

LinearLayout horizontalLayout = new LinearLayout(this);
horizontalLayout.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout verticalLayout1 = new LinearLayout(this);
verticalLayout1.setOrientation(LinearLayout.VERTICAL);
LinearLayout verticalLayout2 = new LinearLayout(this);
verticalLayout2.setOrientation(LinearLayout.VERTICAL);

horizontalLayout.addView(verticalLayout1);
horizontalLayout.addView(verticalLayout2);

只需像在循环中那样使用按钮填充您的第一个和第三个垂直布局。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多