PopupWindow是阻塞对话框,只有在外部线程或者 PopupWindow本身做退出操作才行。PopupWindow完全依赖Layout做外观,在常见的开发中,PopupWindow应该会与AlertDialog常混用。
1.使用 popwindow实现如下效果:
2.代码如下:MainActivity.java
publicclass MainActivity extends Activity {
private Button button;
@Override
publicvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//新建一个按钮
button = (Button) findViewById(R.id.button);
//按钮的监听事件
button.setOnClickListener(new OnClickListener()
{
publicvoid onClick(View v)
{
// 新建一个popwindow,并显示里面的内容
PopupWindow popupWindow = makePopupWindow(MainActivity.this);
//int[] xy = new int[2];
//button.getLocationOnScreen(xy);
//popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,xy[0]/2,xy[1]+button.getWidth());
//popwindow与按钮之间的相对位置
popupWindow.showAsDropDown(button,2, 5);
}
});
}
// 创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx)
{
PopupWindow window;
window = new PopupWindow(cx);