3

我想为两(2)个按钮使用弹出菜单。我现在实现它的方法是制作两个单独的 xml 文件 popup_menu1.xml 和 popup_menu2.xml,它们分别为每个按钮 button1 和 button2 膨胀。Popup-menu-1 有两 (2) 个菜单项,popup-menu-2 有四 (4) 个菜单项。添加图片以进行更多说明。

popup_menu1.xml 有两个菜单项,一个单独的 xml 文件 popup_menu2.xml 有四个菜单项,一个单独的 xml 文件

每个文件都为单个按钮膨胀。

我的问题是:我可以只使用(膨胀)一个(1)个 xml 文件,而不是两个单独的 xml 文件,用于两个(2)个按钮并显示两个(2)个不同的 popup_menus?

任何帮助表示赞赏。

谢谢,

PS:我想添加图片更清楚,但作为新成员他们不允许我这样做。

添加代码:popup_menu1:File1

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu001">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="Today's Date"/>
      <item android:id="@+id/menu2"
          android:title="Custom Date"/>
  </group>
</menu>

popup_menu2:文件2

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menu002">
  <group android:id="@+id/group_popupmenu">
      <item android:id="@+id/menu1"
          android:title="Last Seven (07) Days"/>
      <item android:id="@+id/menu2"
          android:title="Today"/>
      <item android:id="@+id/menu003"
          android:title="Yesterday"/>
      <item android:id="@+id/menu4"
          android:title="Last Twenty Eight (28) Days"/>
  </group>
</menu>

编辑版本 1:活动类中的代码:

// add a click listener to the first button
startDateButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
    PopupMenu popup = new PopupMenu (CampaignDetailsActivity.this, view);
    popup.getMenuInflater().inflate(R.layout.popup_menu_01, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        switch(item.getItemId()){
    case R.id.menu1:
    // some code here
    case R.id.menu2:
    // some code here   
        }
    return true;
    });
 popup.show();
 }
});

// add a click listener to the end date button
endDateButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View button) {
    PopupMenu popup = new PopupMenu (TestAdlikelyButtonsAndMenuActivity.this, button);
            popup.getMenuInflater().inflate(R.layout.popup_menu2, popup.getMenu());

            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    switch(item.getItemId()){
                    case R.id.menu1:
                    // some code here
                    case R.id.menu2:
                    // some code here
                    case R.id.menu003:
                    // some code here
                    case R.id.menu4:
                    // some code here
                    }
                    return true;
                }
            });
        popup.show();
    }
});

图片:

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

1

Yes, you can inflate a single XML file multiple times, but in the example you gave, you're doing it right by having two XML files. After inflating, but before calling PopupMenu.show(), you can use one of the variants of Menu.add() to add items to the Menu (the one returned by PopupMenu.getMenu()).

于 2012-06-11T06:50:06.847 回答