当前位置: 移动技术网 > IT编程>移动开发>IOS > iOS实现底部弹出PopupWindow效果 iOS改变背景透明效果

iOS实现底部弹出PopupWindow效果 iOS改变背景透明效果

2019年07月24日  | 移动技术网IT编程  | 我要评论

底部弹出popupwindow,背景变为半透明效果,采用两种方式实现

先来看看运行效果图

运行效果图
运行效果图

[方式一]实现从底部弹出popupwindow

原理:定义一个高度为wrap_content的popupwindow布局文件,根据屏幕底部的位置显示在bottom

1.首先定义一个高度为wrap_content的popupwindow布局文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:padding="8dp"
  android:orientation="vertical" >

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@drawable/shape_info_bg">
    <textview android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:gravity="center"
      android:textcolor="@color/theme_blue"
      android:text="拍照"/>
    <view android:layout_width="match_parent" 
      android:layout_height="0.2dp"
      android:background="@color/theme_blue"/>
    <textview android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:gravity="center"
      android:textcolor="@color/theme_blue"
      android:text="相冊"/>
  </linearlayout>

  <relativelayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="5dp"
    android:padding="1dp">
    <button
      android:id="@+id/btn_cancle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:padding="8dp"
      android:textcolor="@color/theme_blue"
      android:background="@drawable/shape_info_bg"
      android:text="取消" />
  </relativelayout>
</linearlayout>

2.在popupwindow所在的activity根布局中声明id(在弹出popupwindow作为位置参考的相对view)

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/root_main"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" 
  android:background="@color/white">

  <button android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="底部弹出popupwindow"
    android:onclick="openpop"/>

   <button android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="半透明底部popupwindow"
    android:onclick="openpop2"/>
</linearlayout>

3.mainactivity中点击按钮弹出popupwindow

/** 弹出底部对话框 */
public void openpop(view view) {
  view popview = layoutinflater.from(mcontext).inflate(
      r.layout.pop_bottom, null);
  view rootview = findviewbyid(r.id.root_main); // 當前頁面的根佈局
  final popupwindow popupwindow = new popupwindow(popview,    layoutparams.match_parent,layoutparams.wrap_content);

  setbackgroundalpha(0.5f);//设置屏幕透明度

  popupwindow.setbackgrounddrawable(new bitmapdrawable());
  popupwindow.setfocusable(true);// 点击空白处时,隐藏掉pop窗口
  // 顯示在根佈局的底部
  popupwindow.showatlocation(rootview, gravity.bottom | gravity.left, 0,
      0);

}

4.设置背景变为半透明效果,由于popupwindow中并未设置背景色,所以弹出时popupwindow以外的部分显示当前acitivity的内容,设置背景色原理通过设置当前activity所在屏幕的透明度来达到背景透明的效果!在退出时popupwindow时,需要恢复屏幕原有透明度

popupwindow.setondismisslistener(new ondismisslistener() {
      @override
      public void ondismiss() {
        // popupwindow隐藏时恢复屏幕正常透明度
        setbackgroundalpha(1.0f);
      }
    });
/**
 * 设置添加屏幕的背景透明度
 * 
 * @param bgalpha
 *      屏幕透明度0.0-1.0 1表示完全不透明
 */
public void setbackgroundalpha(float bgalpha) {
  windowmanager.layoutparams lp = ((activity) mcontext).getwindow()
      .getattributes();
  lp.alpha = bgalpha;
  ((activity) mcontext).getwindow().setattributes(lp);
}

[方式二]全屏popupwindow实现底部弹出,背景半透明

原理:弹出一个全屏popupwindow,高度为match_parent,将根布局的gravity属性设置bottom,根布局背景设置为一个半透明的颜色#36000000, 弹出时全屏的popupwindow半透明效果背景覆盖了在activity之上 给人感觉上是popupwindow弹出后,背景变成半透明

布局文件

<?xml version="1.0" encoding="utf-8"?>
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:padding="8dp"
  android:orientation="vertical" 
  android:background="#36000000"
  android:gravity="bottom">

  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical"
    android:background="@drawable/shape_info_bg">
    <textview android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:gravity="center"
      android:textcolor="@color/theme_blue"
      android:text="拍照"/>
    <view android:layout_width="match_parent" 
      android:layout_height="0.2dp"
      android:background="@color/theme_blue"/>
    <textview android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:padding="8dp"
      android:gravity="center"
      android:textcolor="@color/theme_blue"
      android:text="相冊"/>
  </linearlayout>

  <relativelayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margintop="5dp"
    android:padding="1dp">
    <button
      android:id="@+id/btn_cancle"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:padding="8dp"
      android:textcolor="@color/theme_blue"
      android:background="@drawable/shape_info_bg"
      android:text="取消" />
  </relativelayout>
</linearlayout>

弹出方法和方式一相似,不过不用再监听popupwindow的退出事件

/**
 * 弹出底部对话框,达到背景背景透明效果
 * 
 * 实现原理:弹出一个全屏popupwindow,将gravity属性设置bottom,根背景设置为一个半透明的颜色,
 * 弹出时popupwindow的半透明效果背景覆盖了在activity之上 给人感觉上是popupwindow弹出后,背景变成半透明
 */
public void openpop2(view view) {
  view popview = layoutinflater.from(mcontext).inflate(
      r.layout.pop_bottom_alpha, null);
  view rootview = findviewbyid(r.id.root_main); // 當前頁面的根佈局
  final popupwindow popupwindow = new popupwindow(popview,
      layoutparams.match_parent, layoutparams.match_parent);

  // 设置弹出动画
  popupwindow.setanimationstyle(r.style.animationfadebottom);
  popupwindow.setbackgrounddrawable(new bitmapdrawable());
  // 顯示在根佈局的底部
  popupwindow.showatlocation(rootview, gravity.bottom | gravity.left, 0,
      0);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

  • ios uicollectionview实现横向滚动

    现在使用卡片效果的app很多,之前公司让实现一种卡片效果,就写了一篇关于实现卡片的文章。文章最后附有demo实现上我选择了使用uicollectionview ... [阅读全文]
  • iOS UICollectionView实现横向滑动

    本文实例为大家分享了ios uicollectionview实现横向滑动的具体代码,供大家参考,具体内容如下uicollectionview的横向滚动,目前我使... [阅读全文]
  • iOS13适配深色模式(Dark Mode)的实现

    iOS13适配深色模式(Dark Mode)的实现

    好像大概也许是一年前, mac os系统发布了深色模式外观, 看着挺刺激, 时至今日用着也还挺爽的终于, 随着iphone11等新手机的发售, ios 13系统... [阅读全文]
  • ios 使用xcode11 新建项目工程的步骤详解

    ios 使用xcode11 新建项目工程的步骤详解

    xcode11新建项目工程,新增了scenedelegate这个类,转而将原appdelegate负责的对ui生命周期的处理担子接了过来。故此可以理解为:ios... [阅读全文]
  • iOS实现转盘效果

    本文实例为大家分享了ios实现转盘效果的具体代码,供大家参考,具体内容如下demo下载地址: ios转盘效果功能:实现了常用的ios转盘效果,轮盘抽奖效果的实现... [阅读全文]
  • iOS开发实现转盘功能

    本文实例为大家分享了ios实现转盘功能的具体代码,供大家参考,具体内容如下今天给同学们讲解一下一个转盘选号的功能,直接上代码直接看viewcontroller#... [阅读全文]
  • iOS实现轮盘动态效果

    本文实例为大家分享了ios实现轮盘动态效果的具体代码,供大家参考,具体内容如下一个常用的绘图,主要用来打分之类的动画,效果如下。主要是ios的绘图和动画,本来想... [阅读全文]
  • iOS实现九宫格连线手势解锁

    本文实例为大家分享了ios实现九宫格连线手势解锁的具体代码,供大家参考,具体内容如下demo下载地址:效果图:核心代码://// clockview.m// 手... [阅读全文]
  • iOS实现卡片堆叠效果

    本文实例为大家分享了ios实现卡片堆叠效果的具体代码,供大家参考,具体内容如下如图,这就是最终效果。去年安卓5.0发布的时候,当我看到安卓全新的material... [阅读全文]
  • iOS利用余弦函数实现卡片浏览工具

    iOS利用余弦函数实现卡片浏览工具

    本文实例为大家分享了ios利用余弦函数实现卡片浏览工具的具体代码,供大家参考,具体内容如下一、实现效果通过拖拽屏幕实现卡片移动,左右两侧的卡片随着拖动变小,中间... [阅读全文]
验证码:
移动技术网