WebView自定义菜单:移除默认项并在默认项后添加自定义选项
Great questions! Let's tackle them one by one to make your WebView menu setup more robust and user-friendly.
1. Better Alternative to Hardcoded Numeric IDs for Removing Menu Items
Hardcoding those random-looking numeric IDs (51381251 for Share) is super brittle—those numbers can shift across Android versions or even device brands. Instead, use resource identifiers by name, which are far more stable. Here's how:
WebView's default context menu items have standardized resource names in the Android framework, but since the android.webkit.R.id class is hidden from public APIs, you can fetch the IDs using getResources().getIdentifier():
// Retrieve stable IDs for the menu items you want to remove int shareId = getResources().getIdentifier("webmenu_share", "id", "android"); int selectAllId = getResources().getIdentifier("webmenu_selectAll", "id", "android"); int webSearchId = getResources().getIdentifier("webmenu_webSearch", "id", "android"); // Safely remove them (check for 0 to avoid crashes on older Android versions) if (shareId != 0) menu.removeItem(shareId); if (selectAllId != 0) menu.removeItem(selectAllId); if (webSearchId != 0) menu.removeItem(webSearchId);
For even more robustness (in case resource names ever change), you can iterate through the menu and remove items by their localized title:
for (int i = 0; i < menu.size(); i++) { MenuItem item = menu.getItem(i); String itemTitle = item.getTitle().toString(); // Match against localized titles (use system string resources if you can access them) if (itemTitle.equals(getString(R.string.webmenu_share)) || itemTitle.equals(getString(R.string.webmenu_selectAll)) || itemTitle.equals(getString(R.string.webmenu_webSearch))) { menu.removeItem(item.getItemId()); i--; // Adjust index since we removed an item from the list } }
Note: System string resources for these menu items are hidden, so you might need to look up their values for your target SDKs or use hardcoded fallback text (but test across locales if possible).
2. Adding Custom Menu Items After "Copy" and "Paste"
The trick here is to first let WebView build its default menu, then find the position of the last retained default item (Paste), and insert your custom items right after it. Here's a complete implementation in your onCreateContextMenu override:
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { // Let WebView inflate its default menu first super.onCreateContextMenu(menu, v, menuInfo); // Step 1: Remove unwanted items using the method above int shareId = getResources().getIdentifier("webmenu_share", "id", "android"); int selectAllId = getResources().getIdentifier("webmenu_selectAll", "id", "android"); int webSearchId = getResources().getIdentifier("webmenu_webSearch", "id", "android"); if (shareId != 0) menu.removeItem(shareId); if (selectAllId != 0) menu.removeItem(selectAllId); if (webSearchId != 0) menu.removeItem(webSearchId); // Step 2: Locate the position of the "Paste" item int pastePosition = -1; int pasteId = getResources().getIdentifier("webmenu_paste", "id", "android"); for (int i = 0; i < menu.size(); i++) { if (menu.getItem(i).getItemId() == pasteId) { pastePosition = i; break; } } // Step 3: Insert custom items right after Paste (or end of menu if Paste isn't available) int insertIndex = (pastePosition != -1) ? pastePosition + 1 : menu.size(); // Add first custom item menu.add(Menu.NONE, 1001, insertIndex, "My Custom Option") .setOnMenuItemClickListener(item -> { // Handle your custom action here Toast.makeText(getApplicationContext(), "Custom option clicked!", Toast.LENGTH_SHORT).show(); return true; }); // Add second custom item (adjust insertIndex to keep them in order) menu.add(Menu.NONE, 1002, insertIndex + 1, "Another Custom Option") .setOnMenuItemClickListener(item -> { // Handle second custom action return true; }); }
Key Notes:
- The third parameter of
menu.add()is the order—setting it topastePosition + 1ensures your custom item comes immediately after Paste. - If Paste isn't available (e.g., empty clipboard), custom items get added to the end of the menu automatically.
- Use unique IDs for your custom items (like
1001,1002) to avoid conflicts with default menu item IDs.
内容的提问来源于stack exchange,提问作者Dim

