1.步骤
①创建PopupWindow布局
②创建PopupWindow对象,用三个参数的构造
③PopupWindow四种显示方式
④聚焦:EditText能输入内容
⑤点击范围外关闭PopupWindow
⑥全屏阴影,PopupWindow点击消失监听
⑦进出场动画
private void showPoP() {
View view = LayoutInflater.from(this).inflate(R.layout.layout_pop, null);
PopupWindow window = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//点击范围外关闭
window.setBackgroundDrawable(new ColorDrawable());
window.setBackgroundDrawable(new BitmapDrawable());
window.setOutsideTouchable(true);
//进出场动画
window.setAnimationStyle(R.style.pop_animation_style);
//聚焦
window.setFocusable(true);
//四种显示方式
window.showAsDropDown(button);
window.showAsDropDown(button,200,200);
window.showAsDropDown(cl, Gravity.BOTTOM,0,0);
window.showAtLocation(rv, Gravity.BOTTOM, 0, 0);
//设置阴影
setBackGroundAlpha(0.5f);
//PopupWindow点击消失监听
window.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
setBackGroundAlpha(1f);//0.0-1 从完全透明 到 不透明
}
});
}
//设置阴影
private void setBackGroundAlpha(float alpha){
WindowManager.LayoutParams attributes = getWindow().getAttributes();
attributes.alpha=alpha;
getWindow().setAttributes(attributes);
}
style:
<style name="pop_animation_style">
<item name="android:windowEnterAnimation">@anim/enter</item>
<item name="android:windowExitAnimation">@anim/exit</item>
</style>
anim_exit.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="100%p"
android:toYDelta="100%p" />
<alpha
android:fromAlpha="1.0"
android:toAlpha="0" />
</set>
anim_enter.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500">
<translate
android:fromXDelta="100%p"
android:fromYDelta="100%p"
android:toXDelta="0"
android:toYDelta="0" />
<alpha
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>
网友评论