請按照此代碼。
- 與您一樣,在您的
Activity
中添加工具欄。
- 每個片段
res/menu/menu_one.xml
創建菜單和...
- 添加
onCreateOptionsMenu
和onOptionsItemSelected
在每個片段中,類似這樣的:
。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);// add menu which you created in step 2
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings:
// do stuff, like showing settings fragment
return true;
}
return super.onOptionsItemSelected(item); // important line
}
- 而在
onCreate
添加此類似:setHasOptionsMenu(true);
它就是它! :)
你知道android的基礎知識嗎?顯然你應該在菜單中將這些選項作爲片段的菜單。在標籤中使用的片段......這在官方基本指南中得到了很好的解釋 – Selvin
用於什麼?這都是基礎知識。每個片段可以有自己的菜單,當片段可見時顯示... – Selvin
@Selvin不要這麼粗魯!就好像我對你犯了罪。如果你不能幫助,那麼請不要炫耀,就好像你知道一切! –