PopMenu项目空白

时间:2018-12-06 06:03:33

标签: android android-layout popup

enter image description here

如何在弹出菜单选择中删除多余的空格?有什么办法可以将其删除。我在这里阅读了一些文章并尝试了代码。但这对我不起作用。

    final Button btnwallet = findViewById(R.id.btn_wallet);
    btnwallet.setOnClickListener(new View.OnClickListener() {
        @TargetApi(Build.VERSION_CODES.LOLLIPOP_MR1)
        @Override
        public void onClick(View v) {
            PopupMenu popupMenuWallet = new PopupMenu( MainActivity.this, v, Gravity.NO_GRAVITY, R.attr.actionOverflowMenuStyle, 0 );

            popupMenuWallet.getMenuInflater().inflate(R.menu.menu_wallet, popupMenuWallet.getMenu());

            popupMenuWallet.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    int idwallet = item.getItemId();
                    switch (idwallet){
                        case R.id.menu_wallet1:

                            return  true;
                        case R.id.menu_wallet2:

                            return  true;

                        default:

                        return MainActivity.super.onOptionsItemSelected(item);
                    }
                }
            });
            popupMenuWallet.show();
        }
    });

1 个答案:

答案 0 :(得分:0)

我已经通过使用此代码找到了解决我问题的方法。它删除菜单项中多余的空格。我将其发布在这里是因为任何人都可以使用与我的问题相同的代码。

    rlNews = findViewById(R.id.rl_news);

    btnNews = findViewById(R.id.btn_news);
    btnNews.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            layoutInflaterNews = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);

            View containerNews = (View) layoutInflaterNews.inflate(R.layout.list_item_news, null);

            popupWindowNews = new PopupWindow(containerNews, 290, 330, true);
            popupWindowNews.showAsDropDown(btnNews,0,0);

            containerNews.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                popupWindowNews.dismiss();

                return true;
                }
            });
        }
    });