java使用多个视图以编程/动态方式创建LinearLayout
我有这样一个层次结构:
- 线性布局(水平)
- ImageView
- 线性布局(垂直)
- 文本视图
- 文本视图
- 文本视图
- 文本视图
我希望能够通过迭代添加上面的层次结构,只要可以从数据库中获得数据(使用Parse)
我曾尝试在父LinearLayout下设置ImageView和LinearLayout,但似乎不起作用。这是我在MainActivity中的代码。爪哇:
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
安卓:id="@+id/new_linearLayoutOuter"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="@color/white"
安卓:orientation="horizontal" >
<ImageView
安卓:id="@+id/new_imageViewPP"
安卓:layout_width="55dp"
安卓:layout_height="55dp"
安卓:layout_marginLeft="14dp"
安卓:layout_marginTop="12dp"
安卓:background="@color/black"
安卓:contentDescription="@string/pp" />
<LinearLayout
安卓:id="@+id/new_linearLayoutInner"
安卓:layout_width="fill_parent"
安卓:layout_height="wrap_content"
安卓:background="@color/white"
安卓:orientation="vertical" >
<TextView
安卓:id="@+id/new_textView"
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/movie_title"
安卓:paddingTop="10dp"
安卓:paddingLeft="7dp"
安卓:textSize="15sp" /> <!-- Title of the movie -->
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/review_by"
安卓:paddingTop="3dp"
安卓:paddingLeft="7dp"
安卓:textSize="12sp" /> <!-- By -->
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/movie_stars"
安卓:paddingTop="3dp"
安卓:paddingLeft="7dp"
安卓:textSize="12sp" /> <!-- Rating and date -->
<TextView
安卓:layout_width="wrap_content"
安卓:layout_height="wrap_content"
安卓:text="@string/sample_string"
安卓:maxLines="5"
安卓:scrollbars="vertical"
安卓:paddingTop="10dp"
安卓:paddingLeft="7dp"
安卓:textSize="12sp"
安卓:layout_gravity="center_vertical|right" /> <!-- Review content -->
</LinearLayout>
</LinearLayout>
共 (0) 个答案