編輯:關於 Android 程式設計
專案接近收尾時,客戶提出要調整篩選條件的呈現方式。為了把改動降到最低,翻了不少資料,發現做彈窗效果大家普遍用 PopupWindow。但由於先前的設計走的是 Activity 彈窗路線,直接替換掉牽動太多,於是就來研究怎麼讓 Activity 呈現出與 PopupWindow 相當的效果。廢話不多說,直接進主題。
在 AndroidManifest.xml 中為該 Activity 加上一行 theme 宣告即可:
<activity android:name=".product.MyselfPayProduct" android:screenOrientation="portrait" android:theme="@android:style/Theme.Dialog" >
只換主題往往不夠令人滿意,最常見的問題是寬度被內容撐開。解法是透過 Window 調整:
//窗口對齊螢幕寬度 Window win = this.getWindow(); win.getDecorView().setPadding(0, 0, 0, 0); WindowManager.LayoutParams lp = win.getAttributes(); lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.WRAP_CONTENT; lp.gravity = Gravity.TOP;//設置對話框置頂顯示 win.setAttributes(lp);
把這段寫在 Activity 的 onCreate 或相關回呼中即可。setPadding(0,0,0,0) 是為了清掉 Theme.Dialog 自帶的內距,lp.width 設為 MATCH_PARENT 讓它滿版,而 lp.gravity 則用來改變位置——Android 預設彈窗置中,改成 Gravity.TOP 就能貼齊頂部。
接下來示範如何做出類似微信右上角「+」那種從左上角展開的效果。位置可以透過版面配置中的 android:layout_marginTop="50dp" 微調。由於系統預設的彈出效果實在生硬,這裡補上自訂動畫。
進入動畫:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android" > <scale android:fromXScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXScale="1.0" android:fromYScale="0.0" android:toYScale="1.0" android:duration="200" android:pivotX="0" android:pivotY="10%" /> </set>
退出動畫:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android" > <scale android:fromXScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="0.0" android:duration="200" android:pivotX="0" android:pivotY="10%" /> </set>
這裡的技巧是 Y 軸從 0 放大到 1(退出時反向),並把縮放中心 pivotX 設為 0、pivotY 設為 10%,視覺上就變成從左上角展開再收攏。動畫檔放在 res 下的 anim 資料夾,該資料夾預設不存在,需自行建立。
style.xml:
<resources> <!-- Base application theme, dependent on API level. This theme is replaced by AppBaseTheme from res/values-vXX/styles.xml on newer devices. --> <style name="AppBaseTheme" parent="android:Theme.Light"> <!-- Theme customizations available in newer API levels can go in res/values-vXX/styles.xml, while customizations related to backward-compatibility can go here. --> </style> <!-- Application theme. --> <style name="AppTheme" parent="AppBaseTheme"> <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> <!-- 沒有標題 --> <style name="notitle" parent="AppBaseTheme"> <item name="android:windowNoTitle">true</item> </style> <!-- 類似對話框效果 --> <style name="MyDialogTopRight"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowAnimationStyle">@style/Anim_scale</item> </style> <style name="Anim_scale" parent="@android:style/Animation.Activity"> <item name="android:activityOpenEnterAnimation">@anim/scale_in</item> <item name="android:activityOpenExitAnimation">@anim/scale_out</item> <item name="android:activityCloseEnterAnimation">@anim/scale_in</item> <item name="android:activityCloseExitAnimation">@anim/scale_out</item> </style> </resources>
MyDialogTopRight 把視窗背景設為透明、開啟半透明,並透過 windowAnimationStyle 掛上 Anim_scale。動畫樣式需同時設定 open 與 close 兩組,進出才會一致。
回到 AndroidManifest.xml 把主題換掉:
android:theme="@style/MyDialogTopRight"
至此 Activity 的彈窗風格呈現就完整了。相較於改用 PopupWindow 大改結構,這種做法的改動成本明顯更低。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持本站!
Android 仿美團/淘寶多級分類選單實作範例
本文示範一個兩級分類的彈出式選單,效果類似美團、淘寶、百度糯米的分類頁。當分類數量很多時,用「左側一級、右側二級」的雙欄結構會比多層展開清爽得多。 先看效果:  
與其他 App 交互:Intent 的發送、接收與結果回傳
來源:https://developer.android.com/training/basics/intents/index.html 在 App 裡共享數據是再日常不過
Android AsyncTask 異步任務機制詳解:用法、注意事項與原始碼原理
AsyncTask 是 Android 早期常用的異步任務工具,封裝了 Thread 與 Handler,讓開發者能在背景執行工作,再把結果回传到主線程更新 UI。 基
Android 自定義 View:單行上下滾動公告
在許多 App 首頁,公告欄會以單行方式持續上下輪播。看似簡單,其實需要處理文字切換、動畫位移與定時更新。 實作思路 可以自訂一個 LinearLayout,內部放置兩