我正在使用Context Menu创建一个基本的Android项目,用于活动的各种视图。我创建了4个不同的上下文菜单选项,3个用于屏幕上的不同视图,1个用于父布局。
代码如下:
public class MainActivity extends AppCompatActivity {
TextView tV;
Button button;
CoordinatorLayout baseLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
tV = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
baseLayout = (CoordinatorLayout) findViewById(R.id.base_layout);
registerForContextMenu(fab);
registerForContextMenu(tV);
registerForContextMenu(button);
registerForContextMenu(baseLayout);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
if (v.getId() == R.id.fab)
getMenuInflater().inflate(R.menu.menu_context_fab, menu);
} else if (v.getId() == R.id.textView) {
menu.add(v.getId(), 0, Menu.NONE, "Option 1");
menu.add(v.getId(), 1, Menu.NONE, "Option 2");
menu.add(v.getId(), 2, Menu.NONE, "Option 3");
} else if (v.getId() == R.id.button) {
menu.setHeaderTitle("Setting Menu");
menu.add(v.getId(), 0, Menu.NONE, "Setting 1");
menu.add(v.getId(), 1, Menu.NONE, "Setting 2");
menu.add(v.getId(), 2, Menu.NONE, "Setting 3");
} else if (v.getId() == R.id.base_layout) {
menu.setHeaderTitle("Base Menu");
menu.add(v.getId(), 0, Menu.NONE, "Preference 1");
menu.add(v.getId(), 1, Menu.NONE, "Preference 2");
menu.add(v.getId(), 2, Menu.NONE, "Preference 3");
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle().equals("Action 1")) {
Toast.makeText(getApplicationContext(), "Action 1 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getTitle().equals("Action 2")) {
Toast.makeText(getApplicationContext(), "Action 2 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getTitle().equals("Action 3")) {
Toast.makeText(getApplicationContext(), "Action 3 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getGroupId() == R.id.textView) {
if (item.getItemId() == 0) {
Toast.makeText(getApplicationContext(), "Option 1 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 1) {
Toast.makeText(getApplicationContext(), "Option 2 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 2) {
Toast.makeText(getApplicationContext(), "Option 3 pressed", Toast.LENGTH_SHORT).show();
}
} else if (item.getGroupId() == R.id.button) {
if (item.getItemId() == 0) {
Toast.makeText(getApplicationContext(), "Setting 1 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 1) {
Toast.makeText(getApplicationContext(), "Setting 2 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 2) {
Toast.makeText(getApplicationContext(), "Setting 3 pressed", Toast.LENGTH_SHORT).show();
}
} else if (item.getGroupId() == R.id.base_layout) {
if (item.getItemId() == 0) {
Toast.makeText(getApplicationContext(), "Preference 1 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 1) {
Toast.makeText(getApplicationContext(), "Preference 2 pressed", Toast.LENGTH_SHORT).show();
} else if (item.getItemId() == 2) {
Toast.makeText(getApplicationContext(), "Preference 3 pressed", Toast.LENGTH_SHORT).show();
}
}
return true;
}
}
这应该在每种情况下创建不同的上下文菜单,但是,每当我长按屏幕上的任何视图时,布局上下文菜单(首选项选项)的菜单选项也包含在其中。
例如,如果我长按按钮,弹出的上下文菜单包含:
Setting 1
Setting 2
Setting 3
Preference 1
Preference 2
Preference 3
我不明白为什么我的布局菜单选项被添加到所有其他上下文菜单
menu_content_fab.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.osahub.rachit.contextmenu.MainActivity">
<item
android:id="@+id/action_1"
android:title="Action 1"
app:showAsAction="never"/>
<item
android:id="@+id/action_2"
android:title="Action 2"
app:showAsAction="never"/>
<item
android:id="@+id/action_3"
android:title="Action 3"
app:showAsAction="never"/>