全网整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:400-708-3566

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

底部弹出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  # 底部弹出  # PopupWindow  # 背景  # Android控件PopupWindow模仿ios底部弹窗  # ios实现底部PopupWindow的示例代码(底部弹出菜单)  # 弹出  # 全屏  # 给人  # 设置为  # 对话框  # 背景色  # 两种  # 先来  # 大家多多  # 不透明  # 空白处  # 在弹出  # padding  # gravity  # layout_height  # vertical  # orientation  # dp  # match_parent  # res 


相关文章: 如何注册花生壳免费域名并搭建个人网站?  建站之星好吗?新手能否轻松上手建站?  建站之星客服服务时间及联系方式如何?  长春网站建设制作公司,长春的网络公司怎么样主要是能做网站的?  如何通过远程VPS快速搭建个人网站?  如何用虚拟主机快速搭建网站?详细步骤解析  C++如何将C风格字符串(char*)转换为std::string?(代码示例)  网站制作免费,什么网站能看正片电影?  如何快速生成凡客建站的专业级图册?  如何通过商城自助建站源码实现零基础高效建站?  手机网站制作与建设方案,手机网站如何建设?  如何通过虚拟主机快速搭建个人网站?  如何在建站之星绑定自定义域名?  专业网站设计制作公司,如何制作一个企业网站,建设网站的基本步骤有哪些?  我的世界制作壁纸网站下载,手机怎么换我的世界壁纸?  如何选择可靠的免备案建站服务器?  如何获取上海专业网站定制建站电话?  ,在苏州找工作,上哪个网站比较好?  广州建站公司哪家好?十大优质服务商推荐  如何在阿里云购买域名并搭建网站?  如何在景安云服务器上绑定域名并配置虚拟主机?  javascript中对象的定义、使用以及对象和原型链操作小结  建站之星安装需要哪些步骤及注意事项?  简历在线制作网站免费,免费下载个人简历的网站是哪些?  婚礼视频制作网站,学习*后期制作的网站有哪些?  Android使用GridView实现日历的简单功能  如何在IIS7中新建站点?详细步骤解析  如何快速搭建支持数据库操作的智能建站平台?  如何选购建站域名与空间?自助平台全解析  道歉网站制作流程,世纪佳缘致歉小吴事件,相亲网站身份信息伪造该如何稽查?  建站主机服务器选购指南:轻量应用与VPS配置解析  无锡营销型网站制作公司,无锡网选车牌流程?  企业网站制作费用多少,企业网站空间一般需要多大,费用是多少?  常州自助建站:操作简便模板丰富,企业个人快速搭建网站  家庭服务器如何搭建个人网站?  想学网站制作怎么学,建立一个网站要花费多少?  如何通过.red域名打造高辨识度品牌网站?  如何在Tomcat中配置并部署网站项目?  如何在服务器上三步完成建站并提升流量?  如何通过IIS搭建网站并配置访问权限?  智能起名网站制作软件有哪些,制作logo的软件?  建站之星2.7模板:企业网站建设与h5定制设计专题  大连网站制作公司哪家好一点,大连买房网站哪个好?  建站之星导航如何优化提升用户体验?  如何在搬瓦工VPS快速搭建网站?  简单实现Android验证码  建站主机类型有哪些?如何正确选型  三星网站视频制作教程下载,三星w23网页如何全屏?  香港服务器选型指南:免备案配置与高效建站方案解析  魔方云NAT建站如何实现端口转发? 

您的项目需求

*请认真填写需求信息,我们会在24小时内与您取得联系。