2017-07-06 73 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()

相關問題