Android: 在API 9级别中使用PopupMenu

5
我想在我的应用程序中添加一个PopupMenu,但问题是它也应该在Android 2.3上工作。 我找到了一些帖子,人们建议使用AlertDialog作为替代方案,但我更喜欢PopupMenu ;) 我认为它应该在这个API级别中工作,因为我在几个应用程序中看到了它(我的手机有2.3.5版本,它可以正常工作)。 有没有可能让它起作用?
2个回答

13
  • 您必须在应用程序中导入与以下相同的支持v7: 添加具有资源的库

  • import android.support.v7.widget.PopupMenu;

  • 使用该代码进行编译,然后您的弹出式菜单即兼容安卓2.2及以上版本。


我知道了...有趣的是我之前试过那个...也许当时打错了一个字母...现在它完美地运行了 xD - D4ddy

3

PopupMenu可以在发送电子邮件的方法中尝试使用,您可以根据自己的需要填充xml。

    LayoutInflater inflater = (LayoutInflater)EEActionListDetail.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    Display display = getWindowManager().getDefaultDisplay();

    int width = display.getWidth()/2;
    int height = display.getHeight()/2;

    View pop = inflater.inflate(R.layout.popupemail,null,false);
    pop.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);
    height = pop.getMeasuredHeight();
    width = pop.getMeasuredWidth()+200;
    pu = new PopupWindow(pop,width,height,true);
    pu.showAtLocation(findViewById(R.id.ll3),Gravity.CENTER,1,1);

    Button brnSend = (Button)pu.getContentView().findViewById(R.id.btnSend);
    Button close = (Button)pu.getContentView().findViewById(R.id.close);

    Subject = (EditText)pu.getContentView().findViewById(R.id.subject);
    Message = (EditText)pu.getContentView().findViewById(R.id.message);

    close.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            pu.dismiss();

        }
    });
    brnSend.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            for(int j=0;j<EmailArray.size();j++){
                String EmailSent = EmailArray.get(j);
                SendEmailALL(EmailSent);
            }
        }
    });

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接