不能够添加多个子视图到父视图 [英] Not being able to add multiple child views to parent view

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

问题描述

我想多相对布局添加到线性布局。我使用code以下行。

I am trying to add multiple relative layouts to a Linear layout. I am using the following lines of code.

        LayoutInflater inflator = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        LinearLayout item = (LinearLayout)findViewById(R.id.reviews);

        for(int i=0 ; i<2 ; i++){
            View child = inflator.inflate(R.layout.review_item, null);
            child.setId(i);
            child.setTag(i);
            item.addView(child);
        }

但我只能看见一子视图。谁能告诉我在哪里,我错了。

But I can only see one child view. Can anyone tell me where I am going wrong.

推荐答案

您需要采取的前两行的外循环。你充气的LinearLayout两次,这将覆盖膨胀,而不是增加它的第一个布局。通过将这两行之前,为循环开始,您将添加两个子视图到一个单一的LinearLayout。

You need to take the first two lines outside of the for loop. You're inflating the LinearLayout twice, which overrides the first layout you inflate, rather than adding to it. By putting those two lines before the for loop starts, you'll add both child views to a single LinearLayout.

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

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