Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用 Activity 模擬 Dialog 彈窗:樣式、尺寸與顯示位置的完全控制

編輯:關於 Android 程式設計

專案接近收尾時,客戶提出要調整篩選條件的呈現方式。為了把改動降到最低,翻了不少資料,發現做彈窗效果大家普遍用 PopupWindow。但由於先前的設計走的是 Activity 彈窗路線,直接替換掉牽動太多,於是就來研究怎麼讓 Activity 呈現出與 PopupWindow 相當的效果。廢話不多說,直接進主題。

第一步:讓 Activity 長成 Dialog 的樣子

在 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 大改結構,這種做法的改動成本明顯更低。

以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持本站!

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved