4-VIV-Android之PopupWindow
PopWindow是一個可以在Activity 之上顯示任意View的控制元件。
PopWindow可自定義進出動畫、顯示位置,根據已有檢視對齊顯示,可謂彈框一哥。
例項:在按鈕上面顯示

pop.gif
一、建立及配置
載入檢視-->建立物件-->初始設定
1.程式碼
//1.載入檢視 View popView = LayoutInflater.from(this).inflate(R.layout.pop_show, null); //2.建立物件 mPopupWindow = new PopupWindow(); //3.初始設定 mPopupWindow.setContentView(popView);//設定檢視 mPopupWindow.setWidth(300);//設定寬 mPopupWindow.setHeight(500);//設定高 ////等價於上面四句 //mPopupWindow = new PopupWindow(popView, //LinearLayout.LayoutParams.MATCH_PARENT, //LinearLayout.LayoutParams.MATCH_PARENT); mPopupWindow.setFocusable(true);//可聚焦--預設true mPopupWindow.setTouchable(true);//可觸碰--預設true mPopupWindow.setOutsideTouchable(true);//點外部消失 //在安卓4.4(API 19)測試需要背景(這裡是一個透明色)才能點外部消失,安卓8.1(API 27)則不需要 mPopupWindow.setBackgroundDrawable(ContextCompat.getDrawable(this, R.color.transparent)); //4.設定動畫樣式: mPopupWindow.setAnimationStyle(R.style.slide_anim);//設定mPopWindow進出動畫
2.動畫樣式:
預設動畫是忽然出現,忽然消失的
values/styles.xml
<style name="slide_anim"> <item name="android:windowEnterAnimation">@anim/slide_up</item> <item name="android:windowExitAnimation">@anim/slide_down</item> </style>
anim/slide_down.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!--劃下:--> <translate android:duration="200" android:fromYDelta="0" android:toYDelta="100%"/> </set>
anim/slide_up.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!--劃上:--> <translate android:duration="200" android:fromYDelta="100%" android:toYDelta="0" /> </set>
二、顯示與隱藏
//顯示的適配 CompatShowPop.handle(mPopupWindow, mButton, CompatShowPop.BOTTOM2TOP); mPopupWindow.dismiss();//消失
/** * 作者:張風捷特烈<br/> * 時間:2018/11/1 0001:10:40<br/> * 郵箱:[email protected]<br/> * 說明:以View確定popWindow適配 */ public class CompatShowPop { //pop頂對頂 public static final int TOP2TOP = 0x01; //pop頂對底 public static final int TOP2BOTTOM = 0x02; //pop底對頂 public static final int BOTTOM2TOP = 0x03; /** * 以View確定popWindow適配 * * @param popupWindow popupWindow * @param viewview * @param type型別 */ public static void handle(PopupWindow popupWindow, View view, int type) { //處理PopWindow7.0適配 if (Build.VERSION.SDK_INT < 24) { switch (type) { case TOP2TOP: popupWindow.showAsDropDown(view, 0, -view.getHeight()); break; case TOP2BOTTOM: popupWindow.showAsDropDown(view, 0, 0); break; case BOTTOM2TOP: //popupWindow.showAsDropDown(view, 0, -view.getHeight() - popupWindow.getHeight()); popupWindow.showAtLocation(view, Gravity.CLIP_HORIZONTAL, 0, 0); } } else { int[] location = new int[2]; view.getLocationOnScreen(location); int x = location[0]; int y = location[1]; switch (type) { case TOP2TOP: popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y); break; case TOP2BOTTOM: popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y + view.getHeight()); break; case BOTTOM2TOP: //popupWindow.showAtLocation(view, Gravity.CLIP_HORIZONTAL, 0, 0); popupWindow.showAtLocation(view, Gravity.NO_GRAVITY, x, y - popupWindow.getHeight()); } } } }
三、PopupWindow的簡單封裝
/** * 作者:張風捷特烈<br/> * 時間:2018/11/1 0001:11:42<br/> * 郵箱:[email protected]<br/> * 說明:PopupWindow的封裝 */ public abstract class BasePopWindow extends PopupWindow { private View mRootView; private Context mContext; private SparseArray<View> mViews; public BasePopWindow(Context context) { this(context, -1); } public BasePopWindow(Context context, int animStyleId) { mContext = context; //獲取螢幕尺寸 WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); DisplayMetrics outMetrics = new DisplayMetrics(); wm.getDefaultDisplay().getMetrics(outMetrics); mRootView = LayoutInflater.from(context).inflate(layoutId(), null); mViews = new SparseArray<>(); //基本設定 setContentView(mRootView); setWidth(outMetrics.widthPixels); setHeight(outMetrics.heightPixels); setFocusable(true); setTouchable(true); setOutsideTouchable(true); //沉浸標題欄,全屏顯示 setClippingEnabled(false); setBackgroundDrawable(new BitmapDrawable()); if (animStyleId != -1) { setAnimationStyle(animStyleId);//設定mPopWindow進出動畫 } } /** * 通過viewId獲取控制元件 * * @param viewId pop裡的控制元件id * @param <T>控制元件檢視 * @return 控制元件檢視 */ public <T extends View> T getView(int viewId) { View view = mViews.get(viewId); if (view == null) { view = mRootView.findViewById(viewId); mViews.put(viewId, view); } return (T) view; } public abstract int layoutId(); }
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 備註 |
---|---|---|
V0.1--無 | 2018-11-1 | ofollow,noindex">4-VIV-Android之PopupWindow |
2.更多關於我
筆名 | 微信 | 愛好 | |
---|---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 | 語言 |
我的github | 我的簡書 | 我的CSDN | 個人網站 |
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援