【问题标题】:Creating LinearLayout Programmatically/Dynamically with Multiple Views使用多个视图以编程方式/动态创建 LinearLayout
【发布时间】:2013-12-15 12:43:15
【问题描述】:

我的层次结构是这样的:

  • 线性布局(水平)
    • 图像视图
    • 线性布局(垂直)
      • 文本视图
      • 文本视图
      • 文本视图
      • 文本视图

我希望能够通过迭代添加上面的层次结构,只要有可以从数据库中获取的数据(使用Parse

我尝试将 ImageView 和 LinearLayout 放在父 LinearLayout 下,但它似乎不起作用。这是我在 MainActivity.Java 中的代码:

LinearLayout LL_Outer = (LinearLayout) findViewById(R.id.new_linearLayoutOuter);
LL_Outer.setOrientation(LinearLayout.VERTICAL); // set orientation
LL_Outer.setBackgroundColor(color.white); // set background
// set Layout_Width and Layout_Height
LinearLayout.LayoutParams layoutForOuter = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
LL_Outer.setLayoutParams(layoutForOuter);


LinearLayout LL_Inner = (LinearLayout) findViewById(R.id.new_linearLayoutInner);
LL_Inner.setOrientation(LinearLayout.HORIZONTAL);
LL_Inner.setBackgroundColor(color.white);
LinearLayout.LayoutParams layoutForInner = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LL_Inner.setLayoutParams(layoutForInner);

//LL_Outer.addView(LL_Inner);

ImageView IV = (ImageView) findViewById(R.id.new_imageViewPP);
//IV.getLayoutParams().height = 55;
//IV.getLayoutParams().width = 55;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(14, 12, 0, 0);
params.height = 55;
params.weight = 55;
IV.setBackgroundColor(color.black);
IV.setLayoutParams(params);

LL_Inner.addView(IV);
LL_Outer.addView(LL_Inner);

我不知道我哪里出错了,因为我的代码没有提示任何错误。请帮忙。

编辑:我已经相应地编辑了方向,当我运行应用程序时,它停止工作。并在LogCat中提示错误说“指定的孩子已经有一个父母。你必须先在孩子的父母上调用removeView()。

这是我的 XML:

<LinearLayout
    android:id="@+id/new_linearLayoutOuter"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="horizontal" >

    <ImageView
            android:id="@+id/new_imageViewPP"
            android:layout_width="55dp"
            android:layout_height="55dp"
            android:layout_marginLeft="14dp"
            android:layout_marginTop="12dp"
            android:background="@color/black"
            android:contentDescription="@string/pp" />

    <LinearLayout
        android:id="@+id/new_linearLayoutInner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        android:orientation="vertical" >

        <TextView 
            android:id="@+id/new_textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/movie_title"
            android:paddingTop="10dp"
            android:paddingLeft="7dp"
            android:textSize="15sp" /> <!-- Title of the movie -->

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/review_by"
            android:paddingTop="3dp"
            android:paddingLeft="7dp"
            android:textSize="12sp" /> <!-- By -->

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/movie_stars"
            android:paddingTop="3dp"
            android:paddingLeft="7dp"
            android:textSize="12sp" /> <!-- Rating and date -->

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/sample_string"
            android:maxLines="5"
            android:scrollbars="vertical"
            android:paddingTop="10dp"
            android:paddingLeft="7dp"
            android:textSize="12sp"
            android:layout_gravity="center_vertical|right" /> <!-- Review content -->


    </LinearLayout>

</LinearLayout>

【问题讨论】:

  • 首先,交换内部和外部布局的方向。另外,您所说的 是什么意思“似乎不起作用。” 您什么都没有看到,或者您看到了某些东西但没有看到其他人?
  • 我已经相应地编辑了方向。它不会启动应用程序,而是突然关闭。 LogCat 中记录了以下错误:“指定的孩子已经有一个父母。您必须先在孩子的父母上调用 removeView()。”
  • 我认为您在 XML 文件中已经有了 hierarchy。你尝试重新创建它。那就是问题所在。请也发布您的 XML ..
  • 所以要动态加载的视图不应该在 XML 中定义/创建?有没有办法可以重用它们,比如迭代?我添加了我的 XML。请参见上文。
  • 关于这个错误,视图应该执行类似((ViewGroup) IV.getParent()).removeView(IV); 的操作以在使用newParent.addView(IV) 添加到newParent 之前离开旧父级。

标签: java android android-layout android-linearlayout


【解决方案1】:

您希望以编程方式实现该层次结构。

- LinearLayout(horizontal) - ImageView - LinearLayout(vertical) - TextView - TextView - TextView - TextView

好的,让我们从父LinearLayout开始

LinearLayout parent = new LinearLayout(context);

parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);

//children of parent linearlayout

ImageView iv = new ImageView(context);

LinearLayout layout2 = new LinearLayout(context);

layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
layout2.setOrientation(LinearLayout.VERTICAL);

parent.addView(iv);
parent.addView(layout2);

//children of layout2 LinearLayout

TextView tv1 = new TextView(context);
TextView tv2 = new TextView(context);
TextView tv3 = new TextView(context);
TextView tv4 = new TextView(context);

layout2.addView(tv1);
layout2.addView(tv2);
layout2.addView(tv3);
layout2.addView(tv4);

你完成了:)

【讨论】:

  • 您的代码通过了这个异常:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
  • 如何创建具有 width:0dpheight:100dp 等值的 LayoutParams,与 match_parentwrap_content 不同
  • 我在 removeView() stackoverflow.com/questions/50065768/…987654321@ 上遇到了同样的错误
【解决方案2】:

问题是因为视图已经添加到 XML 文件中的布局中。然后你findViewById(找到它们)并尝试再次将它们添加到布局中。这就是为什么应用程序崩溃并抱怨,视图已经有一个父级并且您不能再次添加它。

【讨论】:

    【解决方案3】:

    在 XML 文件中 LinearLayout 已经有子视图。所以不需要在代码中添加它们。

    【讨论】:

      【解决方案4】:

      我建议您删除 xml 文件并在 java 端使用完整代码。您可以从 java 端以编程方式添加视图。这个来自 xtreemdeveloper,但我为父布局更改了几行。

      // remove this params set it up below
      parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      
      // change the code above into 
      .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
              addContentView(parent,layoutParams);
      // end of my change
      

      完整的代码看起来像这样 =

         LinearLayout parent = new LinearLayout(context);
      
          // remove this params set it up below
          parent.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
      
          // change the code above into 
          .LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
                  addContentView(parent,layoutParams);
          // end of my change
      
          parent.setOrientation(LinearLayout.HORIZONTAL);
      
          //children of parent linearlayout
      
          ImageView iv = new ImageView(context);
      
          LinearLayout layout2 = new LinearLayout(context);
      
          layout2.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
          layout2.setOrientation(LinearLayout.VERTICAL);
      
          parent.addView(iv);
          parent.addView(layout2);
      
          //children of layout2 LinearLayout
      
          TextView tv1 = new TextView(context);
          TextView tv2 = new TextView(context);
          TextView tv3 = new TextView(context);
          TextView tv4 = new TextView(context);
      
          layout2.addView(tv1);
          layout2.addView(tv2);
          layout2.addView(tv3);
          layout2.addView(tv4);
      

      【讨论】:

      • java.lang.IllegalArgumentException: 宽度和高度必须 > 0 使用这个
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多