我以编程方式在Java类中创建了一个FrameLayout
FrameLayout mf = new FrameLayout(this);
mf.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200));
layContent.addView(mf);
我希望使用此FrameLayout来显示GoogleMap,这是我的GoogleMap代码
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(xxxxxxx, supportMapFragment).commit();
如果我使用.xml中的FrameLayout,我们可以在替换中添加id(xxxxx ,但现在我以编程方式使用FrameLayout,那么如何以编程方式将Fragment GoogleMap添加到FrameLayout?
答案 0 :(得分:0)
xxxxxxx
是视图中FrameLayout的ID。
所有View对象都有getId()
方法。
FrameLayout layout = new FrameLayout(this);
// Optional: layout.setId(<some positive value>);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200));
layContent.addView(layout);
FragmentManager fm = getSupportFragmentManager();
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(layout.getId(), supportMapFragment).commit();
如果您想要从API级别17及更高级别设置视图ID,则可以调用
View.generateViewId()