如何实现android自定义弹出菜单?
How to implement android custom popup menu?
我想要设计弹出菜单,其项目可单击,类似于 Android 项目中的下图。非常感谢任何推荐。谢谢你。
您可以将 DialogFragment
用于自定义布局。因此,就像您创建一个片段一样,您也可以创建一个 DialogFragment,它将拥有自己的自定义布局和对话框功能。如下-
public class CustomDialogMenu extends DialogFragment {
//global variables
public static CustomDialogMenu newInstance() {
Bundle args = new Bundle();
CustomDialogMenu customDialogMenu = new CustomDialogMenu();
customDialogMenu.setArguments(args);
return customDialogMenu;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.custom_dialog_menu, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
........
}
您可以像调用任何其他片段一样使用此片段,custom_dialog_menu.xml
将在对话框菜单中包含您想要的自定义视图。
您也可以在片段内调用此对话框。代码如下-
private void showCustomDialogMenu() {
FragmentManager fm = getParentFragmentManager();
CustomDialogMenu customDialogMenu= CustomDialogMenu.newInstance();
customDialogMenu.setTargetFragment(this, 300);
customDialogMenu.show(fm, "custom_dialog_menu");
}
有关详细信息,请遵循 this 代码路径。
编码愉快!!
你必须使用 PopupWindow(popupView, width, height, focusable)
:
首先: 膨胀选项菜单
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.option_menu, menu)
}
第二个:覆盖onOptionsItemSelected
:
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.popup_window -> {
showPopup()
true
}
}
这里是showPopup()
的逻辑:
private fun showPopup() {
val anchor = requireActivity().findViewById<View>(R.id.popup_window) // set the menuOption as anchor so that the popup will display TOP RIGHT of the screen
val inflater = requireContext().getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater // get layoutinflater from the system service
val popupView = inflater.inflate(R.layout.popUp_window_options, null) // inflate the popUp_window_options wictch display on popup
// create the popup window
val width = LinearLayout.LayoutParams.WRAP_CONTENT
val height = LinearLayout.LayoutParams.WRAP_CONTENT
val focusable = true // lets taps outside the popup also dismiss it
val popupWindow = PopupWindow(popupView, width, height, focusable)
popupWindow.elevation = 10f // give it shadow
PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.CENTER)
PopupWindowCompat.setWindowLayoutType( popupWindow ,WindowManager.LayoutParams.FLAG_FULLSCREEN)
R.layout.popUp_window_options
是包含选项的布局。
希望能帮助到你。问任何困惑。
之后你会得到类似的东西:
我想要设计弹出菜单,其项目可单击,类似于 Android 项目中的下图。非常感谢任何推荐。谢谢你。
您可以将 DialogFragment
用于自定义布局。因此,就像您创建一个片段一样,您也可以创建一个 DialogFragment,它将拥有自己的自定义布局和对话框功能。如下-
public class CustomDialogMenu extends DialogFragment {
//global variables
public static CustomDialogMenu newInstance() {
Bundle args = new Bundle();
CustomDialogMenu customDialogMenu = new CustomDialogMenu();
customDialogMenu.setArguments(args);
return customDialogMenu;
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.custom_dialog_menu, container);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
........
}
您可以像调用任何其他片段一样使用此片段,custom_dialog_menu.xml
将在对话框菜单中包含您想要的自定义视图。
您也可以在片段内调用此对话框。代码如下-
private void showCustomDialogMenu() {
FragmentManager fm = getParentFragmentManager();
CustomDialogMenu customDialogMenu= CustomDialogMenu.newInstance();
customDialogMenu.setTargetFragment(this, 300);
customDialogMenu.show(fm, "custom_dialog_menu");
}
有关详细信息,请遵循 this 代码路径。
编码愉快!!
你必须使用 PopupWindow(popupView, width, height, focusable)
:
首先: 膨胀选项菜单
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(R.menu.option_menu, menu)
}
第二个:覆盖onOptionsItemSelected
:
override fun onOptionsItemSelected(item: MenuItem): Boolean = when (item.itemId) {
R.id.popup_window -> {
showPopup()
true
}
}
这里是showPopup()
的逻辑:
private fun showPopup() {
val anchor = requireActivity().findViewById<View>(R.id.popup_window) // set the menuOption as anchor so that the popup will display TOP RIGHT of the screen
val inflater = requireContext().getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater // get layoutinflater from the system service
val popupView = inflater.inflate(R.layout.popUp_window_options, null) // inflate the popUp_window_options wictch display on popup
// create the popup window
val width = LinearLayout.LayoutParams.WRAP_CONTENT
val height = LinearLayout.LayoutParams.WRAP_CONTENT
val focusable = true // lets taps outside the popup also dismiss it
val popupWindow = PopupWindow(popupView, width, height, focusable)
popupWindow.elevation = 10f // give it shadow
PopupWindowCompat.showAsDropDown(popupWindow, anchor, 0, 0, Gravity.CENTER)
PopupWindowCompat.setWindowLayoutType( popupWindow ,WindowManager.LayoutParams.FLAG_FULLSCREEN)
R.layout.popUp_window_options
是包含选项的布局。
希望能帮助到你。问任何困惑。
之后你会得到类似的东西: