【发布时间】:2017-09-20 17:34:50
【问题描述】:
我需要能够以编程方式多次从 xml 文件向我的主布局添加布局。
问题在于处理分配新布局视图的 ID。见代码:
MainFragment.java
private int belowOfWhat = R.id.layout_header;
...
onActivityResult:
LayoutInflater myInflater = LayoutInflater.from(getContext());
RelativeLayout layout = (RelativeLayout) myInflater.inflate(R.layout.assignment_layout, null, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//here problems start
//i want to position the new layout below the previously created layout
params.addRule(RelativeLayout.BELOW, belowOfWhat);
layout.setLayoutParams(params);
mRelativeLayout = rootView.findViewById(R.id.to_do_layout);
mRelativeLayout.addView(layout);
belowOfWhat = generateViewId();
idsOfToDos.add(belowOfWhat);
CheckBox assignmentCheckbox = (CheckBox)
//assignment_checkbox is an id of checkbox in the xml layout I add
rootView.findViewById(R.id.assignment_checkbox);
assignmentCheckbox.setId(belowOfWhat);
assignmentCheckbox.setText(mToDoInfo);
我不知道问题出在哪里,下面是应用程序现在的工作方式:我添加了一个新布局,它正确定位在 layout_header 下方。
但是当我添加第二个或更多布局时,它们在应用程序顶部相互重叠,而不是一个位于另一个下方。
如果您能指导我解决问题,我将不胜感激。
【问题讨论】:
标签: android xml android-layout android-fragments onactivityresult