1.先定義兩個(gè)PopupWindow進(jìn)入退出的動(dòng)畫
<!-- push_bottom_in.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="[http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="100"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
<!-- push_bottom_out.xml -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="[http://schemas.android.com/apk/res/android">
<translate
android:duration="500"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="100"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
2.style文件中創(chuàng)建
<!-- PopWindow的展示與消息 -->
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
<!-- 指定顯示的動(dòng)畫xml -->
<item name="android:windowExitAnimation">@anim/push_buttom_out</item>
<!-- 指定消失的動(dòng)畫xml -->
</style>
3.layout中創(chuàng)建PopupWindow的樣式
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="[http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<LinearLayout
android:id="@+id/id_exit_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:text="確認(rèn)退出?"
android:gravity="center"
android:padding="5dp"/>
<Button
android:id="@+id/but_exit_sure"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:text="確定"
android:textColor="#ff0000" />
<Button
android:id="@+id/but_exit_cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:background="#ffffff"
android:text="取消" />
</LinearLayout>
</RelativeLayout>
4.定義個(gè)自定義類繼承PopupWindow
public class ExitPopupWindow extends PopupWindow implements OnClickListener {
private View mPopupWindow;
private OnItemClickListener mListener;
public ExitPopupWindow(Context context) {
super(context);
initPop(context);
setPopupWindow();
}
private void initPop(Context context) {
// TODO Auto-generated method stub
mPopupWindow = LayoutInflater.from(context).inflate(R.layout.exit_popup, null);
Button butExitSure = (Button) mPopupWindow.findViewById(R.id.but_exit_sure);
Button butExitCancel = (Button) mPopupWindow.findViewById(R.id.but_exit_cancel);
butExitSure.setOnClickListener(this);
butExitCancel.setOnClickListener(this);
}
/**
* 設(shè)置PopupWindow
*/
private void setPopupWindow() {
this.setContentView(mPopupWindow);// 設(shè)置View
this.setWidth(LayoutParams.MATCH_PARENT);// 設(shè)置彈出窗口的寬
this.setHeight(LayoutParams.WRAP_CONTENT);// 設(shè)置彈出窗口的高
this.setFocusable(true);// 設(shè)置彈出窗口可
this.setAnimationStyle(R.style.mypopwindow_anim_style);// 設(shè)置動(dòng)畫
this.setBackgroundDrawable(new ColorDrawable(0x33000000));// 設(shè)置背景透明
mPopupWindow.setOnTouchListener(new OnTouchListener() {// 如果觸摸位置在窗口外面則銷毀
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
int height = mPopupWindow.findViewById(R.id.id_exit_layout).getTop();
int y = (int) event.getY();
if (event.getAction() == MotionEvent.ACTION_UP) {
if (y < height) {
dismiss();
}
}
return true;
}
});
}
/**
* 定義一個(gè)接口,公布出去 在Activity中操作按鈕的單擊事件
*/
public interface OnItemClickListener {
void setOnItemClick(View v);
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.mListener = listener;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mListener != null) {
mListener.setOnItemClick(v);
}
}
}
5.在要使用的activity繼承接口OnItemClickListener
mPop = new ExitPopupWindow(SettingActivity.this);
mPop.setOnItemClickListener(SettingActivity.this);
//setting 是要彈出PopupWindow的界面的父布局(就是最外層的布局)
mPop.showAtLocation(SettingActivity.this.findViewById(R.id.setting), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL,0, 0);
@Override
public void setOnItemClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.but_exit_sure:
//搞事情
break;
case R.id.but_exit_sure:
mPop.dismiss();
break;
default:
break;
}
}

hh.png