activity控制對話框風(fēng)格、顯示大小與位置
項(xiàng)目開發(fā)的需要,因?yàn)榈浆F(xiàn)在項(xiàng)目接近完工,用戶提出對條件篩選方式進(jìn)行修改,為做到最小的改動(dòng)實(shí)現(xiàn)用戶的需求,各種百度,對于對話框風(fēng)格大家普遍使用PopupWindow,但由于之前開發(fā)設(shè)計(jì)時(shí)使用的是activity對話框方式,所以今天就為大家介紹一下,如何通過activity實(shí)現(xiàn)與PopupWindow相同的效果,廢話不多講現(xiàn)在開始干貨。
實(shí)現(xiàn)對話框風(fēng)格的activity,我們需要在AndroidManifest.xml添加一句樣式聲明:
<activity android:name=".product.MyselfPayProduct" android:screenOrientation="portrait" android:theme="@android:style/Theme.Dialog" >
不過這樣的對話框風(fēng)格往往無法滿足我們的需要,顯示的效果不那么令人滿意,第一點(diǎn)就是如何控制對話框的大小:
//窗口對齊屏幕寬度 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;//設(shè)置對話框置頂顯示 win.setAttributes(lp);
將這個(gè)控制語句添加在我們的對話框activity的onClick()方法中,這樣我們的對話框就可以寬度與屏幕一樣寬了,lp.gravity = Gravity.TOP;//設(shè)置對話框置頂顯示,android默認(rèn)對話框居中顯示,我們可以通過這句代碼設(shè)置對話框的顯示位置。
到這里是不是已經(jīng)達(dá)到你的滿意了呢?下面在給大家介紹一下,如何通過activity實(shí)現(xiàn)微信右上角點(diǎn)擊加號(hào)的顯示效果。做這個(gè)顯示效果,我們需要通過在布局文件中通過android:layout_marginTop="50dp"這樣來調(diào)整對話框的位置,Android默認(rèn)彈出框效果非常難看,為了達(dá)到更好的顯示效果,我們這里添加一個(gè)顯示的動(dòng)畫效果:
進(jìn)入動(dòng)畫:
<?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>
退出動(dòng)畫:
<?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動(dòng)畫文件一般置于res的anim文件夾下,默認(rèn)該文件夾不存在,需要我們手動(dòng)添加。
下面我們需要把我們的動(dòng)畫添加的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>
<!-- 沒有標(biāo)題 -->
<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"
到這里我們就完美實(shí)現(xiàn)了activity的對話框風(fēng)格顯示。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
Android使用BottomNavigationBar實(shí)現(xiàn)導(dǎo)航欄功能
這篇文章主要介紹了Android使用BottomNavigationBar實(shí)現(xiàn)導(dǎo)航欄功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08
Android系統(tǒng)實(shí)現(xiàn)DroidPlugin插件機(jī)制
這篇文章主要為大家詳細(xì)介紹了Android系統(tǒng)上實(shí)現(xiàn)DroidPlugin插件機(jī)制,可以在無需安裝、修改的情況下運(yùn)行APK文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上
這篇文章主要為大家詳細(xì)介紹了Android調(diào)用系統(tǒng)攝像頭拍照并顯示在ImageView上,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04
Android開發(fā)技巧之永不關(guān)閉的Toast信息框(長時(shí)間顯示而非系統(tǒng)關(guān)閉)
Toast信息提示框之所以在顯示一定時(shí)間后會(huì)自動(dòng)關(guān)閉,是因?yàn)樵谙到y(tǒng)中有一個(gè)Toast隊(duì)列;那么有些時(shí)候需要這個(gè)Toast信息提示框長時(shí)間顯示,直到需要關(guān)閉它時(shí)通過代碼來控制,而不是讓系統(tǒng)自動(dòng)來關(guān)閉Toast信息提示框2013-01-01
Android開發(fā)中類加載器DexClassLoader的簡單使用講解
這篇文章主要介紹了Android開發(fā)中類加載器DexClassLoader的簡單使用講解,DexClassLoader可以看作是一個(gè)特殊的Java中的ClassLoader,需要的朋友可以參考下2016-04-04
Android MarginDesign控件TabLayout導(dǎo)航欄使用詳解
這篇文章主要為大家詳細(xì)介紹了Android MarginDesign控件TabLayout導(dǎo)航欄使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01
RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法
這篇文章主要介紹了RecyclerView焦點(diǎn)跳轉(zhuǎn)BUG優(yōu)化的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04

