与多个视图创建的LinearLayout编程/动态 [英] Creating LinearLayout Programmatically/Dynamically with Multiple Views
问题描述
我有一个层次结构是这样的:
- 的LinearLayout(水平)
- 的ImageView
- 的LinearLayout(垂直)
- 的TextView
- 的TextView
- 的TextView
- 的TextView
我希望能够只要通过迭代加入上面的层次结构有可能从数据库中(使用解析)获得的数据
我试图搭建父的LinearLayout下的ImageView和的LinearLayout,但它似乎并没有工作。这里是我的code在MainActivity.Java:
的LinearLayout LL_Outer =(的LinearLayout)findViewById(R.id.new_linearLayoutOuter);
LL_Outer.setOrientation(LinearLayout.VERTICAL); //设置方向
LL_Outer.setBackgroundColor(color.white); //设置背景
//设置Layout_Width和Layout_Height
LinearLayout.LayoutParams layoutForOuter =新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 =新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 =新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);
我不知道我哪里错了,因为我的code没有提示任何错误。请帮助。
编辑:我已相应编辑的取向,当我运行应用程序,它停止工作。并提示在LogCat中错误说指定的小孩已经有一个家长,你必须首先对孩子的父母打电话removeView()。
下面是我的XML:
<的LinearLayout
机器人:ID =@ + ID / new_linearLayoutOuter
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/白
机器人:方向=横向> < ImageView的
机器人:ID =@ + ID / new_imageViewPP
机器人:layout_width =55dp
机器人:layout_height =55dp
机器人:layout_marginLeft =14dp
机器人:layout_marginTop =12dp
机器人:背景=@彩色/黑白
机器人:contentDescription =@字符串/ PP/> <的LinearLayout
机器人:ID =@ + ID / new_linearLayoutInner
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:背景=@色/白
机器人:方向=垂直> <的TextView
机器人:ID =@ + ID / new_textView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ MOVIE_TITLE
机器人:paddingTop =10dp
机器人:paddingLeft =7DP
机器人:TEXTSIZE =15sp/> <! - 影片的标题 - > <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ review_by
机器人:paddingTop =3DP
机器人:paddingLeft =7DP
机器人:TEXTSIZE =12SP/> <! - 通过 - > <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ movie_stars
机器人:paddingTop =3DP
机器人:paddingLeft =7DP
机器人:TEXTSIZE =12SP/> <! - 等级和日期 - > <的TextView
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=@字符串/ sample_string
机器人:MAXLINES =5
机器人:滚动条=垂直
机器人:paddingTop =10dp
机器人:paddingLeft =7DP
机器人:TEXTSIZE =12SP
机器人:layout_gravity =center_vertical |右/> <! - 评论内容 - >
< / LinearLayout中>< / LinearLayout中>
您想要的层次编程。
- 的LinearLayout(水平)
- ImageView的
- 的LinearLayout(垂直)
- TextView中
- TextView中
- TextView中
- 的TextView
确定可以开始与父的LinearLayout
的LinearLayout父=新的LinearLayout(背景);parent.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.HORIZONTAL);//父的LinearLayout的孩子ImageView的IV =新ImageView的(上下文);的LinearLayout布局2 =新的LinearLayout(背景);layout2.setLayoutParams(新LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
parent.setOrientation(LinearLayout.VERTICAL);parent.addView(四);
parent.addView(布局2);//布局2的LinearLayout儿童TextView的TV1 =新的TextView(背景);
TextView中TV2 =新的TextView(背景);
TextView的TV3 =新的TextView(背景);
TextView的TV4 =新的TextView(背景);layout2.addView(TV1);
layout2.addView(TV2);
layout2.addView(TV3);
layout2.addView(TV4);
和你做:)
I have a hierarchy that is like this:
- LinearLayout(horizontal)
- ImageView
- LinearLayout(vertical)
- TextView
- TextView
- TextView
- TextView
I want to be able to add the hierarchy above through iteration as long as there is data that could be obtained from the database(using Parse)
I have tried putting up the ImageView and LinearLayout under the parent LinearLayout but it doesn't seem to work. Here is my code in 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);
I don't know where I went wrong as my code did not prompt any error. Please help.
EDIT: I have edited the Orientations accordingly and when I run the app, it stops working. And prompts an error in LogCat saying "The specified child already has a parent. You must call removeView() on the child's parent first.
Here's my 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>
You want that hierarchy programmatically.
- LinearLayout(horizontal)
- ImageView
- LinearLayout(vertical)
- TextView
- TextView
- TextView
- TextView
Ok lets start with Parent 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));
parent.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);
And you are done :)
这篇关于与多个视图创建的LinearLayout编程/动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!