activity控制对话框风格、显示大小与位置
项目开发的需要,因为到现在项目接近完工,用户提出对条件筛选方式进行修改,为做到最小的改动实现用户的需求,各种百度,对于对话框风格大家普遍使用PopupWindow,但由于之前开发设计时使用的是activity对话框方式,所以今天就为大家介绍一下,如何通过activity实现与PopupWindow相同的效果,废话不多讲现在开始干货。
实现对话框风格的activity,我们需要在AndroidManifest.xml添加一句样式声明:
<activity android:name=".product.MyselfPayProduct" android:screenOrientation="portrait" android:theme="@android:style/Theme.Dialog">
不过这样的对话框风格往往无法满足我们的需要,显示的效果不那么令人满意,第一点就是如何控制对话框的大小:
//窗口对齐屏幕宽度 Windowwin=this.getWindow(); win.getDecorView().setPadding(0,0,0,0); WindowManager.LayoutParamslp=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默认弹出框效果非常难看,为了达到更好的显示效果,我们这里添加一个显示的动画效果:
进入动画:
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns: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>
退出动画:
<?xmlversion="1.0"encoding="utf-8"?> <setxmlns: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> <!-- Baseapplicationtheme,dependentonAPIlevel.Thisthemeisreplaced byAppBaseThemefromres/values-vXX/styles.xmlonnewerdevices. --> <stylename="AppBaseTheme"parent="android:Theme.Light"> <!-- ThemecustomizationsavailableinnewerAPIlevelscangoin res/values-vXX/styles.xml,whilecustomizationsrelatedto backward-compatibilitycangohere. --> </style> <!--Applicationtheme.--> <stylename="AppTheme"parent="AppBaseTheme"> <!--AllcustomizationsthatareNOTspecifictoaparticularAPI-levelcangohere.--> </style> <!--没有标题--> <stylename="notitle"parent="AppBaseTheme"> <itemname="android:windowNoTitle">true</item> </style> <!--类似对话框效果--> <stylename="MyDialogTopRight"> <itemname="android:windowBackground">@android:color/transparent</item> <itemname="android:windowIsTranslucent">true</item> <itemname="android:windowNoTitle">true</item> <itemname="android:windowAnimationStyle">@style/Anim_scale</item> </style> <stylename="Anim_scale"parent="@android:style/Animation.Activity"> <itemname="android:activityOpenEnterAnimation">@anim/scale_in</item> <itemname="android:activityOpenExitAnimation">@anim/scale_out</item> <itemname="android:activityCloseEnterAnimation">@anim/scale_in</item> <itemname="android:activityCloseExitAnimation">@anim/scale_out</item> </style> </resources>
最后我们需要修改一下我们在AndroidManifest.xml文件中的声明:
android:theme="@style/MyDialogTopRight"
到这里我们就完美实现了activity的对话框风格显示。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持毛票票!