单击按钮时显示弹出菜单

show popup menu when click on button

单击布局时出现弹出菜单问题,有经验的人可以帮助我吗?

我想我的问题可能出在我的主 activity 上,因为我使用的是群组视图,但我不知道如何解决这个问题,有人可以帮助我吗

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity().getApplicationContext();
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fg_dashboard__product__services, container, false);
        rsview = (RecyclerView)view.findViewById(R.id.dashboard_product_service);


        albumList = new ArrayList<>();
        adapter = new DashboardProductServiceAdapter(context, albumList);

        RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(context, 1);
        rsview.setLayoutManager(mLayoutManager);
//       rsview.addItemDecoration(new GridSpacingItemDecoration(2, dpToPx(10), true));
        rsview.setItemAnimator(new DefaultItemAnimator());
        rsview.setAdapter(adapter);

        prepareAlbums();
        return view;

    }

这是我的点击操作

holder.layout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupMenu(holder.layout);
                Toast.makeText(mContext, "Click on layout", Toast.LENGTH_SHORT).show();
            }
        });

这是我的 showPopupMenu 函数。

public void showPopupMenu(View view) {
        PopupMenu popup = new PopupMenu(mContext, view);
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.dashboard_context_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
        popup.show();
    }

您的问题显然不在 java 代码中,您在 XML 中的元素之一没有 layout_height 属性(可能是弹出菜单,或 R.menu.dashboard_context_menu).检查此文件第 17 行是否有任何代码错误。

您需要在 xml 代码中定义 "android:layout_height" 属性。

现在我找到了解决方案,问题不在于 xml 文件,而是在于 java.

我用:

android.widget.PopupMenu popup = new PopupMenu(getApplicationContext(), view);

而不是:

PopupMenu popup = new PopupMenu(mContext, view);