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

所属分类: 软件编程 / Android 阅读数: 22
收藏 0 赞 0 分享

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

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持脚本之家!

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多