从PopupMenu中的MenuItem获取视图

时间:2017-01-05 14:34:06

标签: android coordinates menuitem popupmenu

我需要获取在PopupMenu中单击的MenuItem的坐标。我知道这样做的唯一方法是从MenuItem获取一个View,但我不知道该怎么做。我尝试了这个,但它返回null:

View view = ((SelectPlanningActivity) context).getWindow().getDecorView().findViewById(R.id.action_edit_planning);

我的PopupMenu的完整代码(它位于RecyclerView的适配器中):

PopupMenu popup = new PopupMenu(context, v, Gravity.RIGHT);
            MenuInflater inflater = popup.getMenuInflater();
            inflater.inflate(R.menu.menu_planning_card, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    View view = ((SelectPlanningActivity) context).getWindow().getDecorView().findViewById(R.id.action_edit_planning);
                    int[] coordinates = Utils.getCenterCoordinates(context, view); // I need to use the View here or use a different way to get its position on the screen
                    Intent i = new Intent(context, EditPlanningActivity.class);
                    i.putExtra("planning", planning);
                    i.putExtra("startX", coordinates[0]);
                    i.putExtra("startY", coordinates[1]);
                    ((SelectPlanningActivity) context).startActivityForResult(i, Utils.ACTIVITY_RESULT_EDIT_PLANNING);
                    return true;
                }
            });
            popup.show();

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

尝试:

MenuItem menuitem =popup.getMenu().findItem(R.id.yourmenuitemid);                                                              
menuitem.getActionView().setBackgroundColor(getResources().getColor(R.color.app_menuitem orange));