popupWindow的简单应用
我的需求是点击按钮之后再页面底部弹出popupWindow有删除和取消,点击取消就隐藏popupWindow,点击删除则再弹出一个dialog用来确认是否删除。网上写popupWindow的文章太多了,我这个也是借鉴了别的文章,只是忘了是谁的了。就当做个记录吧!
直接上代码吧。
private void showPopupWindow(View view) {
if (popupWindow != null && popupWindow.isShowing()) {
return;
}
View layout = LayoutInflater.from(this.getActivity()).inflate(R.layout.window_popup, null);
popupWindow = new PopupWindow(layout,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
//点击空白处时,隐藏掉pop窗口
popupWindow.setFocusable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//添加弹出、弹入的动画
popupWindow.setAnimationStyle(R.style.Popupwindow);
int[] location = new int[2];
view.getLocationOnScreen(location);
popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.BOTTOM, 0, -location[1]);
//添加按键事件监听
setButtonListeners(layout);
// //添加pop窗口关闭事件,主要是实现关闭时改变背景的透明度
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1.0f);
}
});
backgroundAlpha(0.618f);
}
/**
* 设置窗口的背景透明度
*
* @param f 0.0-1.0
*/
private void backgroundAlpha(float f) {
WindowManager.LayoutParams layoutParams = getActivity().getWindow().getAttributes();
layoutParams.alpha = f;
getActivity().getWindow().setAttributes(layoutParams);
}
/**
* popupWindow内点击事件
*
* @param layout
*/
private void setButtonListeners(View layout) {
final TextView Delete = (TextView) layout.findViewById(R.id.delete);
TextView Cancel = (TextView) layout.findViewById(R.id.cancel);
Delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (popupWindow != null && popupWindow.isShowing()) {
//点击删除按钮 弹出一个提示框
AlertDialog.Builder builder = new AlertDialog.Builder(MusicPlayFragment.this.getActivity());
builder.setMessage("Are you sure want to delete this music?");
builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//点击确认按钮以后需要做的事情
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog b = builder.create();
//让弹出框以外的区域不能获取焦点
b.setCanceledOnTouchOutside(false);
b.show();
popupWindow.dismiss();
}
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (popupWindow != null && popupWindow.isShowing()) {
//隐藏popupWindow
popupWindow.dismiss();
}
}
});
}
中间有用到一些动画
定义在styles里面
<style name="Popupwindow">
<item name="android:windowEnterAnimation">@anim/window_in</item>
<item name="android:windowExitAnimation">@anim/window_out</item>
</style>
window_in
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromYDelta="100%" android:toYDelta="0"
android:duration="300"/>
window_out
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="0" android:toYDelta="100%"
android:duration="200"/>
这个方法是建立在有点击视图view被点击的时候才有效。可以是按钮 文字 图片等。只要有点击监听都可以点击的。比如说
changeMode.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
showPopupWindow(view);
}
});
好了 基本就这样了,记录下来。