案例效果:
activity_main.xml
popup_window_layout.xml
MainActivity.java
package com.example.administrator.myapplication; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.DisplayMetrics; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.Toast; //PopupWindow public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } //按钮事件方法 public void showWindow(View v) { View view=getLayoutInflater().inflate(R.layout.popup_window_layout,null); //实例化创建popupWindow(窗体的视图,宽,高)对象 PopupWindow popupWindow=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); popupWindow.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); popupWindow.setAnimationStyle(android.R.style.Animation_Translucent);//设置动画效果 popupWindow.getBackground().setAlpha(100);//设置透明度 popupWindow.setOutsideTouchable(true);//点击边上时候可以消失 popupWindow.setFocusable(true);//当前窗口获得焦点 popupWindow.setTouchable(true);//可以被触摸 popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);//防止虚拟软键盘被弹出菜单遮住 popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0); //获取设备屏幕尺寸,4.1之后使用 DisplayMetrics dm=new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); int width=dm.widthPixels; int height=dm.heightPixels; //按钮事件 Button button_edit= (Button)view.findViewById(R.id.button_edit); Button button_delete= (Button)view.findViewById(R.id.button_delete); button_edit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"你点击的是编辑操作",Toast.LENGTH_SHORT).show(); } }); button_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(MainActivity.this,"你点击的是删除操作",Toast.LENGTH_SHORT).show(); } }); } }