所以我一直在努力和Android應用程序的頂部有一個導航欄與幾個選項卡,該部分工作正常,但現在我想能夠動態地添加菜單項目從不同碎片中的操作欄(因爲一些碎片可能有不同的選項)。到目前爲止,無論我嘗試過什麼,我都無法獲得要調用的onCreateOptionsMenu。這裏是我到目前爲止從SherlockActionBar Fragment選項卡動態添加菜單項
//First I have a holder class that is used to navigate between the different Fragment Tabs
public class ActionHolder extends SherlockFragmentActivity implements ActionBar.TabListener {....
//And then I have this method for switching Fragments based on what Tab is selected
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
int selectedTab = tab.getPosition();
if (selectedTab == 0) {
SalesMainScreen salesScreen = new SalesMainScreen();
ft.replace(R.id.content, salesScreen);
}
else if (selectedTab == 1) {
ClientMainScreen clientScreen = new ClientMainScreen();
ft.replace(R.id.content, clientScreen);
}.....
現在這裏是標籤的片段(該SalesMainScreen),我想有幾個菜單項添加到操作欄
@Override
public void onCreate (Bundle savedInstanceState) {
Log.i("message","the oncreate method was called");
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup group, Bundle saved) {
return inflater.inflate(R.layout.salesmainscreen, group, false);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.i("message", "the oncreatemenu method called");
inflater.inflate(R.menu.menu_refresh, menu);
super.onCreateOptionsMenu(menu, inflater);
}
我看到OnCreate中的一個日誌消息被調用,但我沒有看到onCreateOptionsMenu日誌被調用。此外,我知道有時導入會導致問題,但是當我導入Sherlock Menu和Menu Inflater時,我會在OnCreateOptionMenu方法上獲得關於它們不兼容的各種錯誤消息。是否可以在此設置中將菜單項動態添加到操作欄中,或者我應該只添加項目,然後對那些不適用於正在顯示的片段的項目執行任何操作?
可能必須與super.onCreate(savedInstanceState);追隨setHasOptionsMenu(true); ?會覆蓋一切嗎? – RdPC