如何实现android自定义弹出菜单?

问题描述

我想设计弹出菜单,其菜单项可单击,类似于Android项目中的下图。任何建议都非常感激。谢谢。

enter image description here

解决方法

您必须使用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,focusable)
    popupWindow.elevation = 10f // give it shadow

    PopupWindowCompat.showAsDropDown(popupWindow,anchor,Gravity.CENTER)
    PopupWindowCompat.setWindowLayoutType( popupWindow,WindowManager.LayoutParams.FLAG_FULLSCREEN)

R.layout.popUp_window_options是包含选项的布局。 希望能帮助到你。问任何困惑。

此后,您将得到类似:Layout Changed

,

您可以将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代码路径。

快乐编码!

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...