activity控制對話框風格、顯示大小與位置
項目開發(fā)的需要,因為到現在項目接近完工,用戶提出對條件篩選方式進行修改,為做到最小的改動實現用戶的需求,各種百度,對于對話框風格大家普遍使用PopupWindow,但由于之前開發(fā)設計時使用的是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的對話框風格顯示。
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持腳本之家!
相關文章
Android使用BottomNavigationBar實現導航欄功能
這篇文章主要介紹了Android使用BottomNavigationBar實現導航欄功能,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08Android系統(tǒng)實現DroidPlugin插件機制
這篇文章主要為大家詳細介紹了Android系統(tǒng)上實現DroidPlugin插件機制,可以在無需安裝、修改的情況下運行APK文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01Android調用系統(tǒng)攝像頭拍照并顯示在ImageView上
這篇文章主要為大家詳細介紹了Android調用系統(tǒng)攝像頭拍照并顯示在ImageView上,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-04-04Android開發(fā)技巧之永不關閉的Toast信息框(長時間顯示而非系統(tǒng)關閉)
Toast信息提示框之所以在顯示一定時間后會自動關閉,是因為在系統(tǒng)中有一個Toast隊列;那么有些時候需要這個Toast信息提示框長時間顯示,直到需要關閉它時通過代碼來控制,而不是讓系統(tǒng)自動來關閉Toast信息提示框2013-01-01Android開發(fā)中類加載器DexClassLoader的簡單使用講解
這篇文章主要介紹了Android開發(fā)中類加載器DexClassLoader的簡單使用講解,DexClassLoader可以看作是一個特殊的Java中的ClassLoader,需要的朋友可以參考下2016-04-04Android MarginDesign控件TabLayout導航欄使用詳解
這篇文章主要為大家詳細介紹了Android MarginDesign控件TabLayout導航欄使用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-01-01