在这里我给大家介绍一种自定义popup菜单,主要效果是点击一个按钮,会出现一个popup菜单,你可以对其进行选择。
![](https://img-my.csdn.net/uploads/201207/04/1341334397_4522.png)
![](https://img-my.csdn.net/uploads/201207/04/1341334418_9770.png)
网上已经有人写了一个模板,我们可以对其进行适当的修改即可。
原来的代码链接如下:
我修改之后的核心代码如下:
popup类
BetterPopupWindow.java
public class BetterPopupWindow {
protected final View anchor;
private final PopupWindow window;
private View root;
private Drawable background = null;
private final WindowManager windowManager;
/**
* Create a BetterPopupWindow
*
* @param anchor
* the view that the BetterPopupWindow will be displaying 'from'
*/
public BetterPopupWindow(View anchor) {
this.anchor = anchor;
this.window = new PopupWindow(anchor.getContext());
// when a touch even happens outside of the window
// make the window go away
this.window.setTouchInterceptor( new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_OUTSIDE) {
BetterPopupWindow. this.window.dismiss();
return true;
}
return false;
}
});
this.windowManager = (WindowManager) this.anchor.getContext().getSystemService(Context.WINDOW_SERVICE);
onCreate();
}
/**
* Anything you want to have happen when created. Probably should create a view and setup the event listeners on
* child views.
*/
protected void onCreate() {}
/**
* In case there is stuff to do right before displaying.
*/
protected void onShow() {}
private void preShow() {
if( this.root == null) {
throw new IllegalStateException( "setContentView was not called with a view to display.");
}
onShow();
if( this.background == null) {
this.window.setBackgroundDrawable( new BitmapDrawable());
} else {
this.window.setBackgroundDrawable( this.background);
}
// if using PopupWindow#setBackgroundDrawable this is the only values of the width and hight that make it work
// otherwise you need to set the background of the root viewgroup
// and set the popupwindow background to an empty BitmapDrawable
this.window.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
this.window.setTouchable( true);
this.window.setFocusable( true);
this.window.setOutsideTouchable( true);
this.window.setContentView( this.root);
}
public void setBackgroundDrawable(Drawable background) {
this.background = background;
}
/**
* Sets the content view. Probably should be called from {@link onCreate}
*
* @param root
* the view the popup will display
*/
public void setContentView(View root) {
this.root = root;
this.window.setContentView(root);
}
/**
* Will inflate and set the view from a resource id
*
* @param layoutResID
*/
public void setContentView( int layoutResID) {
LayoutInflater inflator =
(LayoutInflater) this.anchor.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setContentView(inflator.inflate(layoutResID, null));
}
/**
* If you want to do anything when {@link dismiss} is called
*
* @param listener
*/
public void setOnDismissListener(PopupWindow.OnDismissListener listener) {
this.window.setOnDismissListener(listener);
}
/**
* Displays like a popdown menu from the anchor view
*/
public void showLikePopDownMenu() {
this.showLikePopDownMenu(0, 0);
}
/**
* Displays like a popdown menu from the anchor view.
*
* @param xOffset
* offset in X direction
* @param yOffset
* offset in Y direction
*/
public void showLikePopDownMenu( int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_PopDownMenu);
this.window.showAsDropDown( this.anchor, xOffset, yOffset);
}
/**
* Displays like a QuickAction from the anchor view.
*/
public void showLikeQuickAction() {
this.showLikeQuickAction(0, 0);
}
/**
* Displays like a QuickAction from the anchor view.
*
* @param xOffset
* offset in the X direction
* @param yOffset
* offset in the Y direction
*/
public void showLikeQuickAction( int xOffset, int yOffset) {
this.preShow();
this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
int[] location = new int[2];
this.anchor.getLocationOnScreen(location);
Rect anchorRect =
new Rect(location[0], location[1], location[0] + this.anchor.getWidth(), location[1]
+ this.anchor.getHeight());
this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
int rootWidth = this.root.getMeasuredWidth();
int rootHeight = this.root.getMeasuredHeight();
int screenWidth = this.windowManager.getDefaultDisplay().getWidth();
int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
int xPos = ((screenWidth - rootWidth) / 2) + xOffset;
Log.e( "TAG", "screenWidth:"+screenWidth+ "");
Log.e( "TAG", "rootWidth:"+rootWidth+ "");
Log.e( "TAG", "xOffset:"+xOffset+ "");
Log.e( "TAG", "xPos:"+xPos+ "");
int yPos = anchorRect.top - rootHeight + yOffset;
Log.e( "TAG", "anchorRect.top:"+anchorRect.top+"" );
Log.e( "TAG", "rootHeight:"+rootHeight+ "");
Log.e( "TAG", "yOffset:"+yOffset+ "");
Log.e( "TAG", "yPos:"+yPos+ "");
// display on bottom
if(rootHeight > anchorRect.top) {
yPos = anchorRect.bottom + yOffset;
Log.e( "TAG", "rootHeight > anchorRect.top,yPos:"+yPos+"");
this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
}
this.window.showAtLocation( this.anchor, Gravity.NO_GRAVITY, xPos, yPos);
}
public void dismiss() {
this.window.dismiss();
}
activity类
PopupMenuActivity.java
public class PopupMenuActivity extends Activity {
Button likemenu,likequickaction;
private static String[] item= new String[]{ "菜单1","菜单2" ,"菜单3" ,"菜单4" ,"菜单5" };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView(R.layout. main);
likemenu=(Button)findViewById(R.id. likemenu);
likequickaction=(Button)findViewById(R.id.likequickaction);
likemenu.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DemoPopupWindow dw= new DemoPopupWindow(v);
dw.showLikePopDownMenu();
}
});
likequickaction.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
DemoPopupWindow dw= new DemoPopupWindow(v);
dw.showLikeQuickAction(0, -100);
}
});
}
/**
* Extends {@link BetterPopupWindow}
* <p>
* Overrides onCreate to create the view and register the button listeners
*
* @author qbert
*
*/
private static class DemoPopupWindow extends BetterPopupWindow implements OnItemClickListener {
public DemoPopupWindow(View anchor) {
super(anchor);
}
@Override
protected void onCreate() {
// inflate layout
LayoutInflater inflater =
(LayoutInflater) this. anchor.getContext().getSystemService(Context. LAYOUT_INFLATER_SERVICE);
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.popup_layout , null);
List<Map<String,Object>> listItems= new ArrayList<Map<String,Object>>();
for( int i=0;i< item. length;i++){
Map<String,Object> listItem= new HashMap<String,Object>();
listItem.put( "item", item[i]);
listItems.add(listItem);
}
//创建adapter
SimpleAdapter simpleAdapter= new SimpleAdapter(this.anchor .getContext(),
listItems,
R.layout. popup_layout_lv,
new String[]{ "item"},
new int[]{R.id. tv}
);
ListView listView = (ListView)root.findViewById(R.id.lv );
listView.setAdapter(simpleAdapter);
//绑定listView点击事件
listView.setOnItemClickListener( this);
// set the inflated view as what we want to display
this.setContentView(root);
}
@Override
public void onItemClick(AdapterView<?> adapterview, View view, int position,
long id) {
// TODO Auto-generated method stub
Toast. makeText(this.anchor.getContext(), "你选择了"+item [position], Toast.LENGTH_LONG).show();
}
}
}
效果图如下:
![](https://img-my.csdn.net/uploads/201207/04/1341334397_4522.png)
![](https://img-my.csdn.net/uploads/201207/04/1341334418_9770.png)
代码下载