添加子视图相对布局视图、布局

2023-09-07 23:41:50 作者:昨日悲喜

虽然对于相对布局和添加子视图编程我经历过很多问题,我无法来解决这个问题。

 的for(int i = 0; I<意见;我++){

    ImageView的IMG =新ImageView的(这一点);
    的LayoutParams img_params =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    img_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    relativeLayout.addView(IMG,img_params);

    TextView中的TextView =新的TextView(本);
    的LayoutParams text_params =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    text_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    relativeLayout.addView(TextView的,text_params);
}
 

下面

我添加日志:

  11月6日至27日:16:38.849:E / AndroidRuntime(20595):java.lang.IllegalStateException:指定的孩子已经有一个父。你必须先调用removeView()对孩子的家长。
 

解决方案

创建的ImageView的新实例的TextView 在循环中

 的for(int i = 0; I<意见;我++){
    的LayoutParams img_params =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    img_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    ImageView的IMG =新ImageView的(这一点);
    relativeLayout.addView(IMG,img_params);

    的LayoutParams text_params =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    text_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    TextView中的TextView =新的TextView(本);
    relativeLayout.addView(TextView的,text_params);
}
 
Android4种布局详解

Though I have been through many questions regarding relative layout and adding child view programatically, I am unable to resolve this issue

for (int i=0; i<views; i++) {

    ImageView img = new ImageView(this);
    LayoutParams img_params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    img_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT|RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    relativeLayout.addView(img, img_params);

    TextView textview = new TextView(this);
    LayoutParams text_params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    text_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT|RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    relativeLayout.addView(textview, text_params);
}

I have added log below:

06-27 11:16:38.849: E/AndroidRuntime(20595): java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

解决方案

Create new instances of ImageView and TextView inside the loop

for (int i = 0; i < views; i++) {
    LayoutParams img_params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    img_params.addRule(RelativeLayout.ALIGN_PARENT_LEFT|RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    ImageView img = new ImageView(this);
    relativeLayout.addView(img, img_params);

    LayoutParams text_params= new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    text_params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT|RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
    TextView textview = new TextView(this);
    relativeLayout.addView(textview, text_params);
}