1:先看运行效果: 2:直接上代码:MainActivity.java package com.youpeng.popwindowdemo; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.LinearLayout; import android.widget.PopupWindow; public class MainActivity extends Activity implements OnClickListener { private Button button; private static final int FIRST = 1; private static final int SECOND = 2; private PopupWindow window; private View view; private Button btnSms, btnEmail, btnCall, btnChat; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 显示 popupWindow window = CreatePopupWindow(MainActivity.this); // PopupWindow popupWindow = makePopupWindow(MainActivity.this); int[] xy = new int[2]; button.getLocationOnScreen(xy); window.showAtLocation(button, Gravity.RIGHT | Gravity.TOP, -xy[0] / 2, xy[1] + button.getWidth()); // popupWindow.showAsDropDown(button,0, 0); } }); } // 创建一个包含自定义view的PopupWindow,通过xml布局 private PopupWindow CreatePopupWindow(MainActivity mainActivity) { view = this.getLayoutInflater().inflate(R.layout.dialog2, null); window = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); btnSms = (Button) view.findViewById(R.id.btnSms); btnSms.setOnClickListener(this); btnSms.setBackgroundResource(R.drawable.sms_selector); btnCall = (Button) view.findViewById(R.id.btnCall); btnCall.setOnClickListener(this); btnCall.setBackgroundResource(R.drawable.call_selector); btnChat = (Button) view.findViewById(R.id.btnChat); btnChat.setOnClickListener(this); btnChat.setBackgroundResource(R.drawable.chat_selector); btnEmail = (