PopupWindow
PopupWindow也是一個對話框,它與Dialog相似,不同的是它可以設定彈出的對話框的位置、大小,也可以通過它彈出動畫等。
//建立PopupWindow對象
popup=new PopupWindow(MainActivity.this);
//獲得布局的view
View popview=getLayoutInflater().inflate(R.layout.popup_layput, null);
//設定對話框的寬度、高度 popup.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popup.setContentView(popview);//設定界面
//此時PopuWindow不能獲得焦點,不能點選外面消失
popup.setFocusable(false);
//設定外部可點選,使對話框dismiss()
popup.setOutsideTouchable(true);
//設定顯示位置,在按鈕2的下方顯示
popup.showAsDropDown(mbtn2);
popup.setAnimationStyle(R.style.AnimationPreview);//設定動畫樣式
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode==KeyEvent.KEYCODE_BACK){
if(popup!=null&&popup.isShowing()){
popup.dismiss();//結束目前PopupWindow
}
}
return true;//傳回true使該事件得到響應