編輯:關於 Android 開發
自定義 PopupWindow 本身並不難,真正讓人頭疼的其實是兩件事:顯示的定位問題和顯示/消失的動畫問題。
定位之所以麻煩,是因為它常常又牽扯到 PopupWindow 的寬高——我們都知道,在 show 之前根本拿不到寬高,可 show 的時候定位又偏偏需要寬高,這就形成了一個矛盾。本文會把這些問題一次性講清楚,並提供三個實用的定位方法:顯示在控件的下左、下中、下右位置。
思路很巧妙:在初始化的時候強制繪制 Layout,就能提前拿到寬高。
// 用於保存PopupWindow的寬度 private int width; // 用於保存PopupWindow的高度 private int height; public CustomPopupWindow(Activity activity) { super(activity); this.activity = activity; this.initPopupWindow(); } private void initPopupWindow() { LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.contentView = inflater.inflate(R.layout.popupwindow_custom, null); this.setContentView(contentView); this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); this.setTouchable(true); this.setFocusable(true); this.setOutsideTouchable(true); this.setAnimationStyle(R.style.PopupAnimation); ColorDrawable background = new ColorDrawable(0x4f000000); this.setBackgroundDrawable(background); this.mandatoryDraw(); } /** * 強制繪制popupWindowView,並且初始化popupWindowView的尺寸 */ private void mandatoryDraw() { this.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); /** * 強制刷新後拿到PopupWindow的寬高 */ this.width = this.contentView.getMeasuredWidth(); this.height = this.contentView.getMeasuredHeight(); }
/** * 顯示在控件的下左方 * * @param parent parent */ public void showAtDropDownLeft(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; //獲取在整個屏幕內的絕對坐標 parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight()); } }
/** * 顯示在控件的下中方 * * @param parent parent */ public void showAtDropDownCenter(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight()); } }
/** * 顯示在控件的下右方 * * @param parent parent */ public void showAtDropDownRight(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight()); } }
這裡設計一個效果:PopWindow 出現時從右上角逐步展開,消失時再向右上角逐步收起。
styles.xml
popwindow_in.xml(以右上角為中心,縮放 0→1 並伴隨透明度漸顯):
<set xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:duration="200" android:fromxscale="0.0" android:fromyscale="0.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="1.0" android:toyscale="1.0"> <alpha android:duration="200" android:fromalpha="0.0" android:toalpha="1.0"> </alpha></scale></set>
popwindow_out.xml(反向操作,縮放 1→0 並伴隨透明度漸隱):
<set xmlns:android="https://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator"> <scale android:duration="200" android:fromxscale="1.0" android:fromyscale="1.0" android:pivotx="100%" android:pivoty="0%" android:toxscale="0.0" android:toyscale="0.0"> <alpha android:duration="200" android:fromalpha="1.0" android:toalpha="0.0"> </alpha></scale></set>
public class CustomPopupWindow extends android.widget.PopupWindow { private Activity activity; private View contentView; private int width; private int height; public CustomPopupWindow(Activity activity) { super(activity); this.activity = activity; this.initPopupWindow(); } private void initPopupWindow() { LayoutInflater inflater = (LayoutInflater) activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); this.contentView = inflater.inflate(R.layout.popupwindow_custom, null); this.setContentView(contentView); this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT); this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT); this.setTouchable(true); this.setFocusable(true); this.setOutsideTouchable(true); this.setAnimationStyle(R.style.PopupAnimation); ColorDrawable background = new ColorDrawable(0x4f000000); this.setBackgroundDrawable(background); this.mandatoryDraw(); } private void mandatoryDraw() { this.contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); this.width = this.contentView.getMeasuredWidth(); this.height = this.contentView.getMeasuredHeight(); } public void showAtDropDownRight(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] + parent.getWidth() - this.width, location[1] + parent.getHeight()); } } public void showAtDropDownLeft(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0], location[1] + parent.getHeight()); } } public void showAtDropDownCenter(View parent) { if (parent.getVisibility() == View.GONE) { this.showAtLocation(parent, 0, 0, 0); } else { int[] location = new int[2]; parent.getLocationOnScreen(location); this.showAtLocation(parent, 0, location[0] / 2 + parent.getWidth() / 2 - this.width / 6, location[1] + parent.getHeight()); } } public static class PopupWindowBuilder { private static String activityHashCode; private static CustomPopupWindow popupWindow; public static PopupWindowBuilder ourInstance; public static PopupWindowBuilder getInstance(Activity activity) { if (ourInstance == null) ourInstance = new PopupWindowBuilder(); String hashCode = String.valueOf(activity.hashCode()); if (!hashCode.equals(String.valueOf(activityHashCode))) { activityHashCode = hashCode; popupWindow = new CustomPopupWindow(activity); } return ourInstance; } public PopupWindowBuilder setTouchable(boolean touchable) { popupWindow.setTouchable(touchable); return this; } public PopupWindowBuilder setAnimationStyle(int animationStyle) { popupWindow.setAnimationStyle(animationStyle); return this; } public PopupWindowBuilder setBackgroundDrawable(Drawable background) { popupWindow.setBackgroundDrawable(background); return this; } public CustomPopupWindow getPopupWindow() { popupWindow.update(); return popupWindow; } } }

自製 ClearEditText:一鍵清空、輸入為空時晃動提示的完整實作
自製 ClearEditText:一鍵清空、輸入為空時晃動提示的完整實作 登入表單常需要兩個小互動:輸入框有內容時右側出現清除按鈕,以及提交時若欄位為空則抖動提示。這兩個
手機安全衛士實作:應用程式管理與使用者/系統程式分類
軟體管理頁是手機安全衛士中很實用的模組,除了列出已安裝應用,還能區分使用者程式與系統程式,並提供分享、執行、卸載與詳情等操作。 取得應用資訊 AppInfos.getA
Android ListView 入門教學與效能優化要點
ListView 是 Android 早期最基礎的列表控制元件,理解它的運作方式與優化技巧,至今仍具參考價值。本文從最簡單的例子出發,示範如何實作並優化 ListView
Ionic 安裝踩雷筆記:從 Node.js 環境到 npm 鏡像的完整解法
Ionic 是什麼 Ionic 是一套前端框架,讓開發者能用 HTML5、CSS3 與 JavaScript 寫出接近原生的行動應用。官方入門文件在:https://io