android 弹出窗口最大高度java

ulmd4ohb  于 2024-01-04  发布在  Android
关注(0)|答案(1)|浏览(170)

我有一个Java代码的弹出菜单,当我按下按钮时激活。它被编码为获取语言和翻译文本(在不同的功能中)转换为不同的语言。由于我有这么多的语言,弹出菜单到达页面的顶部,并且从用户的手机顶部到弹出菜单的开始没有任何空间。有没有一种方法可以编辑菜单的高度,在不改变功能的情况下,只能是最大50 dp(示例)吗?如果是,请添加示例!
下面是弹出窗口的代码:

  1. private void showTranslateDialog() {
  2. PopupMenu popupMenu = new PopupMenu(getContext(), translateBtn);
  3. popupMenu.setGravity(Gravity.BOTTOM);
  4. popupMenu.getMenu().add(Menu.NONE, 1, 1, "Afrikaans");
  5. popupMenu.getMenu().add(Menu.NONE, 2, 2, "Arabic");
  6. popupMenu.getMenu().add(Menu.NONE, 3, 3, "Belarusian");
  7. popupMenu.getMenu().add(Menu.NONE, 4, 4, "Bulgarian");
  8. popupMenu.getMenu().add(Menu.NONE, 5, 5, "Bengali");
  9. popupMenu.getMenu().add(Menu.NONE, 6, 6, "Catalan");
  10. popupMenu.getMenu().add(Menu.NONE, 7, 7, "Czech");
  11. popupMenu.getMenu().add(Menu.NONE, 8, 8, "Welsh");
  12. popupMenu.getMenu().add(Menu.NONE, 9, 9, "Danish");
  13. popupMenu.getMenu().add(Menu.NONE, 10, 10, "German");
  14. popupMenu.getMenu().add(Menu.NONE, 11, 11, "Greek");
  15. popupMenu.getMenu().add(Menu.NONE, 12, 12, "English");
  16. popupMenu.getMenu().add(Menu.NONE, 13, 13, "Esperanto");
  17. popupMenu.getMenu().add(Menu.NONE, 14, 14, "Spanish");
  18. popupMenu.getMenu().add(Menu.NONE, 15, 15, "Estonian");
  19. popupMenu.getMenu().add(Menu.NONE, 16, 16, "Persian");
  20. popupMenu.getMenu().add(Menu.NONE, 17, 17, "Finnish");
  21. popupMenu.getMenu().add(Menu.NONE, 18, 18, "French");
  22. popupMenu.getMenu().add(Menu.NONE, 19, 19, "Irish");
  23. popupMenu.getMenu().add(Menu.NONE, 20, 20, "Galician");
  24. popupMenu.getMenu().add(Menu.NONE, 21, 21, "Gujarati");
  25. popupMenu.getMenu().add(Menu.NONE, 22, 22, "Hebrew");
  26. popupMenu.getMenu().add(Menu.NONE, 23, 23, "Hindi");
  27. popupMenu.getMenu().add(Menu.NONE, 24, 24, "Croatian");
  28. popupMenu.getMenu().add(Menu.NONE, 25, 25, "Haitian");
  29. popupMenu.getMenu().add(Menu.NONE, 26, 26, "Hungarian");
  30. popupMenu.getMenu().add(Menu.NONE, 27, 27, "Indonesian");
  31. popupMenu.getMenu().add(Menu.NONE, 28, 28, "Icelandic");
  32. popupMenu.getMenu().add(Menu.NONE, 29, 29, "Italian");
  33. popupMenu.getMenu().add(Menu.NONE, 30, 30, "Japanese");
  34. popupMenu.getMenu().add(Menu.NONE, 31, 31, "Georgian");
  35. popupMenu.getMenu().add(Menu.NONE, 32, 32, "Kannada");
  36. popupMenu.getMenu().add(Menu.NONE, 33, 33, "Korean");
  37. popupMenu.getMenu().add(Menu.NONE, 34, 34, "Lithuanian");
  38. popupMenu.getMenu().add(Menu.NONE, 35, 35, "Latvian");
  39. popupMenu.getMenu().add(Menu.NONE, 36, 36, "Macedonian");
  40. popupMenu.getMenu().add(Menu.NONE, 37, 37, "Marathi");
  41. popupMenu.getMenu().add(Menu.NONE, 38, 38, "Malay");
  42. popupMenu.getMenu().add(Menu.NONE, 39, 39, "Maltese");
  43. popupMenu.getMenu().add(Menu.NONE, 40, 40, "Dutch");
  44. popupMenu.getMenu().add(Menu.NONE, 41, 41, "Norwegian");
  45. popupMenu.getMenu().add(Menu.NONE, 42, 42, "Polish");
  46. popupMenu.getMenu().add(Menu.NONE, 43, 43, "Portuguese");
  47. popupMenu.getMenu().add(Menu.NONE, 44, 44, "Romanian");
  48. popupMenu.getMenu().add(Menu.NONE, 45, 45, "Russian");
  49. popupMenu.getMenu().add(Menu.NONE, 46, 46, "Slovak");
  50. popupMenu.getMenu().add(Menu.NONE, 47, 47, "Slovenian");
  51. popupMenu.getMenu().add(Menu.NONE, 48, 48, "Albanian");
  52. popupMenu.getMenu().add(Menu.NONE, 49, 49, "Swedish");
  53. popupMenu.getMenu().add(Menu.NONE, 50, 50, "Swahili");
  54. popupMenu.getMenu().add(Menu.NONE, 51, 51, "Tamil");
  55. popupMenu.getMenu().add(Menu.NONE, 52, 52, "Telugu");
  56. popupMenu.getMenu().add(Menu.NONE, 53, 53, "Thai");
  57. popupMenu.getMenu().add(Menu.NONE, 54, 54, "Tagalog");
  58. popupMenu.getMenu().add(Menu.NONE, 55, 55, "Turkish");
  59. popupMenu.getMenu().add(Menu.NONE, 56, 56, "Ukrainian");
  60. popupMenu.getMenu().add(Menu.NONE, 57, 57, "Urdu");
  61. popupMenu.getMenu().add(Menu.NONE, 58, 58, "Vietnamese");
  62. popupMenu.getMenu().add(Menu.NONE, 59, 59, "Chinese");
  63. popupMenu.show();
  64. popupMenu.setOnMenuItemClickListener(menuItem -> {
  65. int id = menuItem.getItemId();
  66. switch (id) {
  67. case 1: translateTo = "af"; identifyLanguage(); break; // Afrikaans
  68. case 2: translateTo = "ar"; identifyLanguage(); break; // Arabic
  69. case 3: translateTo = "be"; identifyLanguage(); break; // Belarusian
  70. case 4: translateTo = "bg"; identifyLanguage(); break; // Bulgarian
  71. case 5: translateTo = "bn"; identifyLanguage(); break; // Bengali
  72. case 6: translateTo = "ca"; identifyLanguage(); break; // Catalan
  73. case 7: translateTo = "cs"; identifyLanguage(); break; // Czech
  74. case 8: translateTo = "cy"; identifyLanguage(); break; // Welsh
  75. case 9: translateTo = "da"; identifyLanguage(); break; // Danish
  76. case 10: translateTo = "de"; identifyLanguage(); break; // German
  77. case 11: translateTo = "el"; identifyLanguage(); break; // Greek
  78. case 12: translateTo = "en"; identifyLanguage(); break; // English
  79. case 13: translateTo = "eo"; identifyLanguage(); break; // Esperanto
  80. case 14: translateTo = "es"; identifyLanguage(); break; // Spanish
  81. case 15: translateTo = "et"; identifyLanguage(); break; // Estonian
  82. case 16: translateTo = "fa"; identifyLanguage(); break; // Persian
  83. case 17: translateTo = "fi"; identifyLanguage(); break; // Finnish
  84. case 18: translateTo = "fr"; identifyLanguage(); break; // French
  85. case 19: translateTo = "ga"; identifyLanguage(); break; // Irish
  86. case 20: translateTo = "gl"; identifyLanguage(); break; // Galician
  87. case 21: translateTo = "gu"; identifyLanguage(); break; // Gujarati
  88. case 22: translateTo = "he"; identifyLanguage(); break; // Hebrew
  89. case 23: translateTo = "hi"; identifyLanguage(); break; // Hindi
  90. case 24: translateTo = "hr"; identifyLanguage(); break; // Croatian
  91. case 25: translateTo = "ht"; identifyLanguage(); break; // Haitian
  92. case 26: translateTo = "hu"; identifyLanguage(); break; // Hungarian
  93. case 27: translateTo = "id"; identifyLanguage(); break; // Indonesian
  94. case 28: translateTo = "is"; identifyLanguage(); break; // Icelandic
  95. case 29: translateTo = "it"; identifyLanguage(); break; // Italian
  96. case 30: translateTo = "ja"; identifyLanguage(); break; // Japanese
  97. case 31: translateTo = "ka"; identifyLanguage(); break; // Georgian
  98. case 32: translateTo = "kn"; identifyLanguage(); break; // Kannada
  99. case 33: translateTo = "ko"; identifyLanguage(); break; // Korean
  100. case 34: translateTo = "lt"; identifyLanguage(); break; // Lithuanian
  101. case 35: translateTo = "lv"; identifyLanguage(); break; // Latvian
  102. case 36: translateTo = "mk"; identifyLanguage(); break; // Macedonian
  103. case 37: translateTo = "mr"; identifyLanguage(); break; // Marathi
  104. case 38: translateTo = "ms"; identifyLanguage(); break; // Malay
  105. case 39: translateTo = "mt"; identifyLanguage(); break; // Maltese
  106. case 40: translateTo = "nl"; identifyLanguage(); break; // Dutch
  107. case 41: translateTo = "no"; identifyLanguage(); break; // Norwegian
  108. case 42: translateTo = "pl"; identifyLanguage(); break; // Polish
  109. case 43: translateTo = "pt"; identifyLanguage(); break; // Portuguese
  110. case 44: translateTo = "ro"; identifyLanguage(); break; // Romanian
  111. case 45: translateTo = "ru"; identifyLanguage(); break; // Russian
  112. case 46: translateTo = "sk"; identifyLanguage(); break; // Slovakian
  113. case 47: translateTo = "sl"; identifyLanguage(); break; // Slovenian
  114. case 48: translateTo = "sq"; identifyLanguage(); break; // Albanian
  115. case 49: translateTo = "sv"; identifyLanguage(); break; // Swedish
  116. case 50: translateTo = "sw"; identifyLanguage(); break; // Swahili
  117. case 51: translateTo = "ta"; identifyLanguage(); break; // Tamil
  118. case 52: translateTo = "te"; identifyLanguage(); break; // Telugu
  119. case 53: translateTo = "th"; identifyLanguage(); break; // Thai
  120. case 54: translateTo = "tl"; identifyLanguage(); break; // Tagalog
  121. case 55: translateTo = "tr"; identifyLanguage(); break; // Turkish
  122. case 56: translateTo = "uk"; identifyLanguage(); break; // Ukrainian
  123. case 57: translateTo = "ur"; identifyLanguage(); break; // Urdu
  124. case 58: translateTo = "vi"; identifyLanguage(); break; // Vietnamese
  125. case 60: translateTo = "zh"; identifyLanguage(); break; // Chinese
  126. }
  127. return true;
  128. });
  129. }
  130. private boolean hasInstallPermission() {
  131. return ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.REQUEST_INSTALL_PACKAGES) == PackageManager.PERMISSION_GRANTED;
  132. }

字符串

ntjbwcob

ntjbwcob1#

res/values/styles.xml中为您创建自定义样式PopupMenu

  1. <style name="CustomPopupMenu" parent="Widget.AppCompat.PopupMenu">
  2. <item name="android:maxHeight">50dp</item>
  3. </style>

字符串
然后在示例化PopupMenu时应用样式

  1. PopupMenu popupMenu = new PopupMenu(new ContextThemeWrapper(context, R.style.CustomPopupMenu), translateBtn);

相关问题