您的位置:首页 > 其它

控制对话框风格的activity的显示大小与位置

2017-04-10 21:46 218 查看
  项目开发的需要,因为到现在项目接近完工,用户提出对条件筛选方式进行修改,为做到最小的改动实现用户的需求,各种百度,对于对话框风格大家普遍使用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的对话框风格显示。

来源:http://www.cnblogs.com/AndroidJotting/p/4768849.html

来自为知笔记(Wiz)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: