2014-11-01 89 views
0

我在android中的Actionbar中使用了Tab。貝婁是我設置onCreateView佈局片段:以編程方式更改Android中的現有佈局

mFragmentView = inflater.inflate(R.layout.second_activity, container, false); 
return mFragmentView; 

我的問題是:我可以通過編程方式改變現有佈局(R.layout.second_activity)與另一個(R.layout.sample_layout)?

回答

0
getFragmentManager().beginTransaction() 
      .detach(this) 
      .attach(this) 
      .commit(); 

這將強制片段再次調用onCreateView。在這裏,您可以返回R.layout.sampleLayout而不是通常的佈局。在調用片段事務之前在片段中設置一個變量,以便在調用此函數時選擇正確的佈局。

第二種解決方案是將您的初始佈局封裝在viewGroup中,然後調用getView.removeAllViews(),膨脹新的佈局並將其添加到根視圖。