private Context mContext;
private View view;
private Button btn_cancel,btn_sure;
public MyPopupwindow(Context mContext, View.OnClickListener itemsOnClick) {
this.view = LayoutInflater.from(mContext).inflate(R.layout.popupwindow_layout, null);
btn_cancel = (Button) view.findViewById(R.id.btn_cancel);
btn_sure= (Button) view.findViewById(R.id.btn_sure);
// 取消按鈕
btn_cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 銷燬彈出框
dismiss();
}
});
btn_sure.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// 銷燬彈出框
dismiss();
}
});
// 設置按鈕監聽
// 設置外部可點擊
this.setOutsideTouchable(true);
// mMenuView添加OnTouchListener監聽判斷獲取觸屏位置如果在選擇框外面則銷燬彈出框
this.view.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
int height = view.findViewById(R.id.pop_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
/* 設置彈出窗口特徵 */
// 設置視圖
this.setContentView(this.view);
// 設置彈出窗體的寬和高
this.setHeight(RelativeLayout.LayoutParams.WRAP_CONTENT);
this.setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
// 設置彈出窗體可點擊
this.setFocusable(true);
// 實例化一個ColorDrawable顏色爲半透明
ColorDrawable dw = new ColorDrawable(0x00000000);
// 設置彈出窗體的背景
this.setBackgroundDrawable(dw);
// 設置彈出窗體顯示時的動畫,從底部向上彈出
this.setAnimationStyle(R.style.take_photo_anim);
}
}
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="200"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="50%p" />
<alpha
android:duration="200"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
<item name="android:windowEnterAnimation">@anim/pop_enter_anim</item>
<item name="android:windowExitAnimation">@anim/pop_exit_anim</item>