不能够添加多个子视图到父视图
问题描述:
我想多相对布局添加到线性布局。我使用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.