添加子视图相对布局 [英] Adding child views to Relative Layout

查看:162
本文介绍了添加子视图相对布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 的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);
}
 

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);
}

这篇关于添加子视图相对布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆