控制對話方塊風格的activity的顯示大小與位置,對話方塊activity
項目開發的需要,因為到現在項目接近完工,使用者提出對條件式篩選方式進行修改,為做到最小的改動實現使用者的需求,各種百度,對於對話方塊風格大家普遍使用PopupWindow,但由於之前開發設計時使用的是activity對話方塊方式,所以今天就為大家介紹一下,如何通過activity實現與PopupWindow相同的效果,廢話不多講現在開始乾貨。
實現對話方塊風格的activity,我們需要在AndroidManifest.xml添加一句樣式聲明:
<activity android:name=".product.MyselfPayProduct" android:screenOrientation="portrait" android:theme="@android:style/Theme.Dialog" >
不過這樣的對話方塊風格往往無法滿足我們的需要,顯示的效果不那麼令人滿意,第一點就是如何控制對話方塊的大小:
//視窗對齊螢幕寬度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的onClick()方法中,這樣我們的對話方塊就可以寬度與螢幕一樣寬了,lp.gravity = Gravity.TOP;//設定對話方塊置頂顯示,android預設對話方塊置中顯示,我們可以通過這句代碼設定對話方塊的顯示位置。
到這裡是不是已經達到你的滿意了呢?下面在給大家介紹一下,如何通過activity實現右上方點擊加號的顯示效果。做這個顯示效果,我們需要通過在布局檔案中通過android:layout_marginTop="50dp"這樣來調整對話方塊的位置,Android預設彈出框效果非常難看,為了達到更好的顯示效果,我們這裡添加一個顯示的動畫效果:
進入動畫:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://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="http://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>
android動畫檔案一般置於res的anim檔案夾下,預設該檔案夾不存在,需要我們手動添加。
下面我們需要把我們的動畫添加的android的樣式檔案: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>
最後我們需要修改一下我們在AndroidManifest.xml檔案中的聲明:
android:theme="@style/MyDialogTopRight"
到這裡我們就完美實現了activity的對話方塊風格顯示。