Android自定义PopupWindow实现炫酷的IOS对话框效果

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

前言:

最近在使用IOS系统的过程中发现IOS底部弹出框甚是漂亮,大气,上档次,于是乎就想啊能不能在Android中实现类似的对话框呢?你说,这不是废话吗,除了一些极少数的系统级的不能模仿外(版权)还有啥不能依瓢画葫芦的呢,所以啊,这篇文章将介绍如何在Android中实现高仿IOS对话框效果,先上图,给大家养养眼:

这里写图片描述

大家在看到上面的对话框时有没有想到简单的实现思路呢?我这里给出的思路是我们可以自定义一个PopupWindow,然后设置我们的布局。这里的布局很有技巧哦,那就是对话框中间的透明隔断区域其实是一个margin值,每个隔断的item layout的背景为一个白色圆角矩形,之后再让PopupWindow的背景为透明即可,是不是很简单呢。好了,让我们动手编写代码将它带回家吧。

大家也可以看看我的上篇文章:Android自定义Dialog,炫酷主流的加载对话框

代码实现

1. 编写布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
 <LinearLayout
 android:layout_width="match_parent"
 android:orientation="horizontal"
 android:paddingTop="12dp"
 android:paddingBottom="12dp"
 android:background="@drawable/corner_white_bg"
 android:layout_height="wrap_content">
 <Button
  android:id="@id/btn_share_weixin"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:drawableTop="@mipmap/ic_share_weixin"
  android:drawablePadding="6dp"
  android:background="@null"
  android:textColor="@android:color/black"
  android:textSize="@dimen/text_14_sp"
  android:text="@string/weixin"
  android:layout_height="wrap_content"/>
 <Button
  android:id="@id/btn_share_friends"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:drawableTop="@mipmap/ic_share_friends"
  android:drawablePadding="6dp"
  android:background="@null"
  android:textColor="@android:color/black"
  android:textSize="@dimen/text_14_sp"
  android:text="@string/weixin_friends"
  android:layout_height="wrap_content"/>
 <Button
  android:id="@id/btn_share_qq"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:drawableTop="@mipmap/ic_share_qq"
  android:drawablePadding="6dp"
  android:background="@null"
  android:textColor="@android:color/black"
  android:textSize="@dimen/text_14_sp"
  android:text="@string/qq"
  android:layout_height="wrap_content"/>
 <Button
  android:id="@id/btn_share_qq_zone"
  android:layout_width="0dp"
  android:layout_weight="1"
  android:drawableTop="@mipmap/ic_share_zones"
  android:drawablePadding="6dp"
  android:textColor="@android:color/black"
  android:textSize="@dimen/text_14_sp"
  android:background="@null"
  android:text="@string/qq_zones"
  android:layout_height="wrap_content"/>
 </LinearLayout>
 <Button
 android:id="@id/btn_cancel"
 android:layout_width="match_parent"
 android:textColor="@android:color/black"
 android:textSize="@dimen/text_14_sp"
 android:layout_marginTop="10dp"
 android:layout_marginBottom="10dp"
 android:text="@string/cancel"
 android:background="@drawable/corner_white_bg"
 android:layout_height="wrap_content"/>
</LinearLayout>

这里被隔断的部分有两个,所以布局中有两个view的背景为白色圆角矩形。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
 <corners android:radius="10dp"/>
 <solid android:color="@android:color/white"/>
</shape>

2. 继承PopupWindow

public class IosPopupWindow extends PopupWindow implements View.OnClickListener {
 private Context mContext;
 public IosPopupWindow(Activity activity) {
 super(activity);
 mContext = activity;
 LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 View contentView = inflater.inflate(R.layout.dialog_share, null);
 setContentView(contentView);
 int screenWidth = activity.getWindowManager().getDefaultDisplay().getWidth();
 //获取popupwindow的高度与宽度
 this.setWidth((int) (screenWidth - 2 * dp2px(mContext,12f)));
 this.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
 // 设置背景透明度
 setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
 // 设置动画
 this.setAnimationStyle(R.style.IosDialog);
 // 设置弹出窗体可点击
 this.setFocusable(true);
 // 点击外部可取消
 this.setOutsideTouchable(true);
 initView(contentView);
 }

以上代码最关键的就是给我们的PopupWindow设置一个透明的背景Drawable啦。

3. 窗口弹出时让外部变暗

/**
 * 让popupwindow以外区域阴影显示
 */
private void popOutShadow() {
 final Window window = ((Activity) mContext).getWindow();
 WindowManager.LayoutParams lp = window.getAttributes();
 lp.alpha = 0.5f;//设置阴影透明度
 window.setAttributes(lp);
 setOnDismissListener(new OnDismissListener() {
 @Override
 public void onDismiss() {
  WindowManager.LayoutParams lp = window.getAttributes();
  lp.alpha = 1f;
  window.setAttributes(lp);
 }
 });
}

与Dialog不同的是PopupWindow实现外部变暗需通过改变它依附的window的透明度,所以我们传给PopupWindow的Context需为Activity类型,同时在窗口消失的时候记得将Window的透明度重置。

最后,奉上IosPopupWindow的github,你值得拥有:https://github.com/ydxlt/LoadingDialog

以上所述是小编给大家介绍的Android自定义PopupWindow实现炫酷的IOS对话框效果,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

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

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 分享
查看更多