答案 0 :(得分:3)
您可以创建PopupMenu
。
首先在popup_menu.xml
文件夹中创建res/menu
文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
tools:context=".MainActivity">
<item
android:id="@+id/action_copy"
android:orderInCategory="100"
android:title="@string/action_copy"/>
<item
android:id="@+id/action_forvard"
android:orderInCategory="110"
android:title="@string/action_forvard"/>
</menu>
然后在您的视图的onClickListener的PopupMenu
方法中实现onClick()
:
@Override
public void onClick(View view) {
PopupMenu popup = new PopupMenu(MainActivity.this, view);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_copy:
//your code here
break;
case R.id.action_forvard:
//your code here
break;
}
return true;
}
});
popup.show();
}