2017-07-06 54 views
0

我编程在Java类如何以编程方式添加谷歌地图到的FrameLayout

FrameLayout mf = new FrameLayout(this); 
mf.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 200)); 
layContent.addView(mf); 

取得了FrameLayout里,我想用这个FrameLayout里作秀GoogleMap的,这是我的GoogleMap

FragmentManager fm = getSupportFragmentManager(); 
SupportMapFragment supportMapFragment = SupportMapFragment.newInstance(); 
fm.beginTransaction().replace(xxxxxxx, supportMapFragment).commit(); 
代码

如果我使用的FrameLayout从.XML,我们可以在添加ID替换(XXXXX,但现在我用的FrameLayout编程,那么如何添加片段的GoogleMap以编程的FrameLayout?

回答

0

xxxxxxx是视图中FrameLayout的ID。

所有查看对象都有一个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(); 

如果要任意地设定观看ID,从API级别17以上,你可以调用

View.generateViewId()

相关问题