安卓PopupWindow
生活随笔
收集整理的這篇文章主要介紹了
安卓PopupWindow
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文由PurpleSword(jzj1993)原創,轉載請注明 原文網址 http://blog.csdn.net/jzj1993
定義彈窗動畫
res/anim/scale_in.xml
<?xml?version="1.0"?encoding="utf-8"?> <set?xmlns:android="http://schemas.android.com/apk/res/android"?> ????<scale ????????android:duration="300" ????????android:fromXScale="0.0" ????????android:fromYScale="0.0" ????????android:pivotX="50%" ????????android:pivotY="50%" ????????android:toXScale="1.0" ????????android:toYScale="1.0"?/> ????<alpha ????????android:duration="300" ????????android:fromAlpha="0.0" ????????android:toAlpha="1.0"?/> </set>?
res/anim/scale_out.xml
<?xml?version="1.0"?encoding="utf-8"?> <set?xmlns:android="http://schemas.android.com/apk/res/android"?> ????<scale ????????android:duration="200" ????????android:fromXScale="1.0" ????????android:fromYScale="1.0" ????????android:pivotX="50%" ????????android:pivotY="50%" ????????android:toXScale="0.0" ????????android:toYScale="0.0"?/> ????<alpha ????????android:duration="200" ????????android:fromAlpha="1.0" ????????android:toAlpha="0.0"?/> </set>?
res/values/styles.xml
? ??<style?name="pop_scale"> ????????<item?name="android:windowEnterAnimation">@anim/scale_in</item> ????????<item?name="android:windowExitAnimation">@anim/scale_out</item> ????</style>??
程序實現
? ??private?PopupWindow?pop;
? ??void?showPop()?{ ????????if?(pop?!=?null?&&?pop.isShowing())?{ ????????????return; ????????}
? ? ? ??LayoutInflater?i?=?LayoutInflater.from(this); ????????View?v?=?i.inflate(R.layout.main_menu_popupwnd,?null);
? ? ? ??//?指定寬度和高度 固定值 ? ? ? ??pop?=?new?PopupWindow(v,?100,?100);
? ? ? ??//?指定寬度和高度 LayoutParams參數 ? ? ? ??pop?=?new?PopupWindow(v,?LayoutParams.WRAP_CONTENT, ????????????????LayoutParams.WRAP_CONTENT);
? ? ? ??//?設置彈窗之外是否可點擊(點擊后彈窗消失) ????????pop.setOutsideTouchable(true); ? ? ? ??// 要實現點擊彈窗外部使得彈窗消失,需要設置背景 ????????pop.setBackgroundDrawable(new?ColorDrawable(0));
? ? ? ??//?設置動畫 ????????pop.setAnimationStyle(R.style.pop_scale);
? ? ? ??// 彈窗消失事件的監聽器 ????????pop.setOnDismissListener(new?PopupWindow.OnDismissListener()?{ ????????????@Override ????????????public?void?onDismiss()?{ ???????????????? ????????????} ????????});
? ? ? ??// 刷新設置 ????????pop.update();
? ? ? ??// 顯示 (位置和偏移:靠下方顯示,向上偏移30像素) ? ? ? ??pop.showAtLocation(findViewById(R.id.layout_main_scroll_view), ????????????????Gravity.CENTER_HORIZONTAL?|?Gravity.BOTTOM,?0,?30); ? ? }
? ??boolean?dismissPop()?{ ????????if?(pop?!=?null?&&?pop.isShowing())?{ ????????????pop.dismiss(); ????????????return?true; ????????} ????????return?false; ????}??
按返回鍵、Activity暫停、退出時,確保關閉PopupWindow,否則會報錯
? ??@Override ????public?void?onPause()?{ ????????dismissMenu(); ? ? ? ??super.onPause(); ????}
定義彈窗動畫
res/anim/scale_in.xml
<?xml?version="1.0"?encoding="utf-8"?> <set?xmlns:android="http://schemas.android.com/apk/res/android"?> ????<scale ????????android:duration="300" ????????android:fromXScale="0.0" ????????android:fromYScale="0.0" ????????android:pivotX="50%" ????????android:pivotY="50%" ????????android:toXScale="1.0" ????????android:toYScale="1.0"?/> ????<alpha ????????android:duration="300" ????????android:fromAlpha="0.0" ????????android:toAlpha="1.0"?/> </set>?
res/anim/scale_out.xml
<?xml?version="1.0"?encoding="utf-8"?> <set?xmlns:android="http://schemas.android.com/apk/res/android"?> ????<scale ????????android:duration="200" ????????android:fromXScale="1.0" ????????android:fromYScale="1.0" ????????android:pivotX="50%" ????????android:pivotY="50%" ????????android:toXScale="0.0" ????????android:toYScale="0.0"?/> ????<alpha ????????android:duration="200" ????????android:fromAlpha="1.0" ????????android:toAlpha="0.0"?/> </set>?
res/values/styles.xml
? ??<style?name="pop_scale"> ????????<item?name="android:windowEnterAnimation">@anim/scale_in</item> ????????<item?name="android:windowExitAnimation">@anim/scale_out</item> ????</style>??
程序實現
? ??private?PopupWindow?pop;
? ??void?showPop()?{ ????????if?(pop?!=?null?&&?pop.isShowing())?{ ????????????return; ????????}
? ? ? ??LayoutInflater?i?=?LayoutInflater.from(this); ????????View?v?=?i.inflate(R.layout.main_menu_popupwnd,?null);
? ? ? ??//?指定寬度和高度 固定值 ? ? ? ??pop?=?new?PopupWindow(v,?100,?100);
? ? ? ??//?指定寬度和高度 LayoutParams參數 ? ? ? ??pop?=?new?PopupWindow(v,?LayoutParams.WRAP_CONTENT, ????????????????LayoutParams.WRAP_CONTENT);
? ? ? ??//?設置彈窗之外是否可點擊(點擊后彈窗消失) ????????pop.setOutsideTouchable(true); ? ? ? ??// 要實現點擊彈窗外部使得彈窗消失,需要設置背景 ????????pop.setBackgroundDrawable(new?ColorDrawable(0));
? ? ? ??//?設置動畫 ????????pop.setAnimationStyle(R.style.pop_scale);
? ? ? ??// 彈窗消失事件的監聽器 ????????pop.setOnDismissListener(new?PopupWindow.OnDismissListener()?{ ????????????@Override ????????????public?void?onDismiss()?{ ???????????????? ????????????} ????????});
? ? ? ??// 刷新設置 ????????pop.update();
? ? ? ??// 顯示 (位置和偏移:靠下方顯示,向上偏移30像素) ? ? ? ??pop.showAtLocation(findViewById(R.id.layout_main_scroll_view), ????????????????Gravity.CENTER_HORIZONTAL?|?Gravity.BOTTOM,?0,?30); ? ? }
? ??boolean?dismissPop()?{ ????????if?(pop?!=?null?&&?pop.isShowing())?{ ????????????pop.dismiss(); ????????????return?true; ????????} ????????return?false; ????}??
按返回鍵、Activity暫停、退出時,確保關閉PopupWindow,否則會報錯
? ??@Override ????public?void?onPause()?{ ????????dismissMenu(); ? ? ? ??super.onPause(); ????}
總結
以上是生活随笔為你收集整理的安卓PopupWindow的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 零余额账户和基本户的区别
- 下一篇: 配债缴款怎么操作,什么是股票配债