服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - IOS - iOS实现底部弹出PopupWindow效果 iOS改变背景透明效果

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

2021-03-23 16:00iOS开发网 IOS

这篇文章主要为大家详细介绍了iOS实现底部弹出PopupWindow效果,iOS改变背景透明效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

先来看看运行效果图

 

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

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?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)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** 弹出底部对话框 */
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时,需要恢复屏幕原有透明度

?
1
2
3
4
5
6
7
popupwindow.setondismisslistener(new ondismisslistener() {
      @override
      public void ondismiss() {
        // popupwindow隐藏时恢复屏幕正常透明度
        setbackgroundalpha(1.0f);
      }
    });
?
1
2
3
4
5
6
7
8
9
10
11
12
/**
 * 设置添加屏幕的背景透明度
 *
 * @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弹出后,背景变成半透明

布局文件

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?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的退出事件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
 * 弹出底部对话框,达到背景背景透明效果
 *
 * 实现原理:弹出一个全屏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);
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/LANG791534167/article/details/48985101

延伸 · 阅读

精彩推荐
  • IOSiOS中tableview 两级cell的展开与收回的示例代码

    iOS中tableview 两级cell的展开与收回的示例代码

    本篇文章主要介绍了iOS中tableview 两级cell的展开与收回的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    J_Kang3862021-04-22
  • IOSiOS通过逆向理解Block的内存模型

    iOS通过逆向理解Block的内存模型

    自从对 iOS 的逆向初窥门径后,我也经常通过它来分析一些比较大的应用,参考一下这些应用中某些功能的实现。这个探索的过程乐趣多多,不仅能满足自...

    Swiftyper12832021-03-03
  • IOSiOS 雷达效果实例详解

    iOS 雷达效果实例详解

    这篇文章主要介绍了iOS 雷达效果实例详解的相关资料,需要的朋友可以参考下...

    SimpleWorld11022021-01-28
  • IOSIOS开发之字典转字符串的实例详解

    IOS开发之字典转字符串的实例详解

    这篇文章主要介绍了IOS开发之字典转字符串的实例详解的相关资料,希望通过本文能帮助到大家,让大家掌握这样的方法,需要的朋友可以参考下...

    苦练内功5832021-04-01
  • IOS关于iOS自适应cell行高的那些事儿

    关于iOS自适应cell行高的那些事儿

    这篇文章主要给大家介绍了关于iOS自适应cell行高的那些事儿,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的...

    daisy6092021-05-17
  • IOSiOS布局渲染之UIView方法的调用时机详解

    iOS布局渲染之UIView方法的调用时机详解

    在你刚开始开发 iOS 应用时,最难避免或者是调试的就是和布局相关的问题,下面这篇文章主要给大家介绍了关于iOS布局渲染之UIView方法调用时机的相关资料...

    windtersharp7642021-05-04
  • IOS解析iOS开发中的FirstResponder第一响应对象

    解析iOS开发中的FirstResponder第一响应对象

    这篇文章主要介绍了解析iOS开发中的FirstResponder第一响应对象,包括View的FirstResponder的释放问题,需要的朋友可以参考下...

    一片枫叶4662020-12-25
  • IOSIOS 屏幕适配方案实现缩放window的示例代码

    IOS 屏幕适配方案实现缩放window的示例代码

    这篇文章主要介绍了IOS 屏幕适配方案实现缩放window的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要...

    xiari5772021-06-01