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

所属分类: 软件编程 / Android 阅读数: 49
收藏 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网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多