Android MenuItem Custom Layout

前端 未结 3 823
失恋的感觉
失恋的感觉 2020-12-10 02:14

I have a PopupMenu that appears when I click on an action button in a actionbar. I would like the MenuItem, in my PopupMenu, with a custom layout like this:

layout/m

3条回答
  •  北荒
    北荒 (楼主)
    2020-12-10 03:07

    By default the layout of a PopupMenu (and it's items) cannot be customized. In the solution below I have created a PopupMenu with a horizontal layout. In this case I used TextView as clickable items, but you can easily replace them by Buttons. You can customize the menu items whatever you want.

    1 - The Custom PopupMenu class:

    public class PopupMenuCustomLayout {
        private PopupMenuCustomOnClickListener onClickListener;
        private Context context;
        private PopupWindow popupWindow;
        private int rLayoutId;
        private View popupView;
    
        public PopupMenuCustomLayout(Context context, int rLayoutId, PopupMenuCustomOnClickListener onClickListener) {
            this.context = context;
            this.onClickListener = onClickListener;
            this.rLayoutId = rLayoutId;
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
            popupView = inflater.inflate(rLayoutId, null);
            int width = LinearLayout.LayoutParams.WRAP_CONTENT;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;
            boolean focusable = true;
            popupWindow = new PopupWindow(popupView, width, height, focusable);
            popupWindow.setElevation(10);
    
            LinearLayout linearLayout = (LinearLayout) popupView;
            for (int i = 0; i < linearLayout.getChildCount(); i++) {
                View v = linearLayout.getChildAt(i);
                v.setOnClickListener( v1 -> { onClickListener.onClick( v1.getId()); popupWindow.dismiss(); });
            }
        }
        public void setAnimationStyle( int animationStyle) {
            popupWindow.setAnimationStyle(animationStyle);
        }
        public void show() {
            popupWindow.showAtLocation( popupView, Gravity.CENTER, 0, 0);
        }
    
        public void show( View anchorView, int gravity, int offsetX, int offsetY) {
            popupWindow.showAsDropDown( anchorView, 0, -2 * (anchorView.getHeight()));
        }
    
        public interface PopupMenuCustomOnClickListener {
            public void onClick(int menuItemId);
        }
    }
    

    2 - Your custom layout, e.g. linearlayout with horizontal layout. In this case I use a simple LinearLayout with TextView items. You can use Buttons, etc.

    
    
        
        
        // ...
    
    

    3 - Using the Custom PopupMenu like the normal PopupMenu.

    PopupMenuCustomLayout popupMenu = new PopupMenuCustomLayout(
            MainActivity.mainActivity, R.layout.popup_menu_custom_layout,
            new PopupMenuCustomLayout.PopupMenuCustomOnClickListener() {
                @Override
                public void onClick(int itemId) {
                    // log statement: "Clicked on: " + itemId
                    switch (itemId) {
                        case R.id.popup_menu_custom_item_a:
                            // log statement: "Item A was clicked!"
                            break;
                    }
                }
            });
    // Method 1: popupMenu.show();
    // Method 2: via an anchor view: 
    popupMenu.show( anchorView, Gravity.CENTER, 0, 0);
    

提交回复
热议问题