1

单击操作栏中的按钮后,我试图使 PopupMenu 出现。

我有文件中的按钮 -action.xml 在“ menu”文件夹中。

这包含 ActionBar 的按钮。

<item
    android:id="@+id/overflow"
    android:icon="@drawable/ic_action_overflow"
    android:orderInCategory="2"
    android:menuCategory="container"
    android:title="Overflow Button"       
    android:showAsAction="ifRoom|withText"
    android:onClick="**showPopup**"
    />

<item
    android:id="@+id/add"
    android:icon="@drawable/add"
    android:title="Add Button"
    android:orderInCategory="100"
    android:showAsAction="ifRoom|withText"
    android:onClick="**showPopup**"
    />

单击按钮后,我希望PopupMenu显示一个按钮。该showPopup方法位于FragmentActivity类中:

 public void showPopup(MenuItem v) {        
        PopupMenu popup = new PopupMenu(this, this.getCurrentFocus());      
        popup.inflate(R.layout.pop);        
        popup.show();       
        }

pop.xml文件存储在布局文件夹中(并包含弹出菜单的视图)

不幸的是,这给了我一个RuntimeException java.lang.reflect.InvocationTargetException.

有任何想法吗?

帮助将不胜感激。

4

3 回答 3

1

首先,这很容易。

您必须将此属性添加到您的 actionBar 项。

android:onClick="openPopup"

openPopup是你的弹出方法。

然后,在您MainActivity应该扩展的方式中,ActionBarActivity
您必须添加将被调用的方法onClick

public void openPopup(MenuItem item){ 
      Point p;
      p.x = 0;
      p.y = 0;
      showPopup(MainActivity.this, p);
      Toast.makeText(this, "Popup Should Open", Toast.LENGTH_LONG).show();
  } 

当然还有showPopup方法。

private void showPopup(final Activity context, Point p) {

   // Inflate the popup_layout.xml
   LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
   LayoutInflater layoutInflater = (LayoutInflater) context
     .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
   View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

   // Creating the PopupWindow
   final PopupWindow popup = new PopupWindow(context);
   popup.setContentView(layout);
   popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
   popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
   popup.setFocusable(true);

   popupWidth = popup.getWidth();

   // Clear the default translucent background
   popup.setBackgroundDrawable(new BitmapDrawable(this.getResources()));

   // Displaying the popup at the specified location.
   popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);

   // Getting a reference to Close button, and close the popup when clicked.
   Button close = (Button) layout.findViewById(R.id.close);
   close.setOnClickListener(new OnClickListener() {

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

而已。

于 2013-12-26T19:47:43.507 回答
0

我想分享我的做法。假设我们有一个 id 为“btn”的按钮(或任何 clicakbale 视图),我们想在其点击时附加一个 popupMenu。将要显示的菜单放在以下 xml 中的菜单文件夹中,例如“menu1.xml”。

按钮的 OnClick 定义如下:

Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showPopupMenu(v);
            }
        });

showPopupMenu() 定义如下:

private void showPopupMenu(View view) {
    // inflate menu and attach it to a view onClick of which you want to display menu
    PopupMenu popup = new PopupMenu(mContext, view);
    MenuInflater inflater = popup.getMenuInflater();
    //inflate menu items to popup menu
    inflater.inflate(R.menu.menu1, popup.getMenu());
    //assign a cutom onClick Listener to it.
    popup.setOnMenuItemClickListener(new MyMenuItemClickListener());
    //Show Popup.
    popup.show();
}

完整的帖子可以在这里找到:http ://revisitingandroid.blogspot.in/2017/01/how-to-display-popup-menu-on-any-view.html

于 2017-01-06T06:23:05.703 回答
-1
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_margin="20dip"
        android:text="Popup Menu Example" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Touch here to see the menu."
        android:layout_margin="20dip"
        android:layout_gravity="center"
        android:textColor="#0000ff"
        android:id="@+id/anchor"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:layout_gravity="center"
        android:textSize="40sp"
        android:textColor="#808080"
        android:id="@+id/selection"/>
</LinearLayout>

package com.authorwjf.popmenu;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.app.Activity;
public class Main extends Activity implements OnClickListener, OnMenuItemClickListener
{

       private PopupMenu popupMenu;
       private final static int ONE = 1;
       private final static int TWO = 2;
       private final static int THREE = 3;
       @Override
       protected void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);
             setContentView(R.layout.main);
             popupMenu = new PopupMenu(this, findViewById(R.id.anchor));
             popupMenu.getMenu().add(Menu.NONE, ONE, Menu.NONE, "Item 1");
             popupMenu.getMenu().add(Menu.NONE, TWO, Menu.NONE, "Item 2");
             popupMenu.getMenu().add(Menu.NONE, THREE, Menu.NONE, "Item 3");
             popupMenu.setOnMenuItemClickListener(this);
             findViewById(R.id.anchor).setOnClickListener(this);
       }
}


@Override
public void onClick(View v) {
       popupMenu.show();
}


@Override
public boolean onMenuItemClick(MenuItem item) {
       TextView tv = (TextView) findViewById(R.id.selection);
       switch (item.getItemId()) {
       case ONE:
              tv.setText("ONE");
              break;
       case TWO:
              tv.setText("TWO");
              break;
       case THREE:
              tv.setText("THREE");
              break;
       }
       return false;
}
于 2013-04-05T12:49:04.303 回答