簡體   English   中英

如何實現android自定義彈出菜單?

[英]How to implement android custom popup menu?

我想要設計彈出菜單,其項目可點擊,類似於 Android 項目中的下圖。 任何推薦都非常感謝。 謝謝你。 在此處輸入圖片說明

你必須使用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是包含選項的布局。 希望能幫助到你。 問任何困惑。

在此之后你會得到類似的東西: 這個

您可以將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");
}

有關更多信息,請遵循代碼路徑。

快樂編碼!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM