[英]Tabbed Activity and Menu in Toolbar
我有一个选项卡式活动,其中包含3个带有片段的不同选项卡。 我也有一个工具栏,在这个工具栏中,我有1个静态菜单对象和1个动态对象。
我将静态对象(蓝牙连接)放入MainActivity,其中将Fragments加载到容器中,将动态按钮放入需要该按钮的Fragment中。
问题是,如果我将菜单方法声明为MainActivity,则声明为片段的方法是无用的,当我按图标时,什么也没发生。...但是,如果我删除了管理菜单的方法进入MainActivity,则按钮变成了片段...
有什么办法可以解决这个问题? 还是唯一的方法是对每个片段重复静态按钮,并为每个片段添加其他片段不需要的特定按钮?
MainActivity菜单方法:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_bluetooth:
if (mBluetoothService == null) {
Log.d(TAG, "menu Bluetooth button");
setupComunication();
} else {
mBluetoothService = null;
setupComunication();
}
// Launch the DeviceListActivity to see devices and do scan
Intent serverIntent = new Intent(this, DeviceListActivity.class);
startActivityForResult(serverIntent, REQUEST_CONNECT_DEVICE_INSECURE);
//findDevice();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
片段菜单方法:
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
inflater.inflate(R.menu.menu_dashboard, menu);
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_commands:
// User chose the "Settings" item, show the app settings UI...
Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivityForResult(settingsIntent, RESULT_SETTINGS);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
覆盖在这两种方法Fragment
,添加的项目menu_dashboard
到menu_main
和设置的可视性menuItem
默认为false:
@Override
public void onPrepareOptionsMenu(Menu menu) {
menu.findItem(R.id.action_commands).setVisible(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getItemId()) {
case R.id.action_commands:
// User chose the "Settings" item, show the app settings UI...
Intent settingsIntent = new Intent(getActivity(), SettingsActivity.class);
startActivityForResult(settingsIntent, RESULT_SETTINGS);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.