底部弹出PopupWindow并且背景变为半透明效果

Android中为PopupWindow设置半透明背景的方案(兼容华为手机) 一种设置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);
}





原文地址:

http://blog.csdn.NET/LANG791534167/article/details/48985101

Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow) 本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现。想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果。 一,使用Activity 首先是此activity的布局文件: <RelativeLayout xmlns:android="http://schemas.android.com 阅读详情

相关推荐

Android编程实现popupwindow弹出后屏幕背景变成半透明效果

主要介绍了Android编程实现popupwindow弹出后屏幕背景变成半透明效果,涉及Android设置getWindows透明度的方法,需要的朋友可以参考下

当显示PopupWindow时如何设置透明度

在android中展示PopupWindow时设置透明背景的流程: 1、点击view,更改view的状态(展示状态),然后弹出popwindow。弹出popwindow的同时,设置背景为半透明。 2、操作或查看后关闭,关闭的同时回复view状态(原始状态),同时设置背景为不透明状态。 例子: private MyPopupWindow popupWindow; private void showPopupWindow(int type) { if (popupWind..

lgs7907 4192

【SLAM】SVO2.0编译运行和论文代码解读

SVO2是我遇到的最难编译的代码了,可能公开版本不能放太鲁棒的代码吧,搞了快一个星期,终于跑起来了。

iwanderu的博客 5974

仿IOS底部选项效果--底部弹出PopupWindow,背景变为半透明效果

底部弹出PopupWindow,背景变为半透明效果,采用两种方式实现 转载请注明出处:http://blog.youkuaiyun.com/LANG791534167/article/details/48985101 先来看看运行效果图 [方式一]实现从底部弹出PopupWindow原理:定义一个高度为wrap_content的PopupWindow布局文件,根据屏幕底部的位置显示在Bottom1.首

萧萧北keno的专栏 6596

【cocos2dx面试题干货】--2021年最新cocos2dx面试干货(引擎篇)

【cocos2dx面试题干货】--2020年度最新cocos2dx面试干货(引擎篇 ) 大家好,我是Lampard~~ 经过春招一番艰苦奋战之后,我终于是进入了心仪的公司。 今天给大家分享一下我在之前精心准备的一套面试知识。 由于我面试的岗位是cocos2dx的开发岗,今天就先给大家分享一下引擎方面的知识。 本人亲测80%的引擎相关题目都...

Lampard的博客 1万+

Qt Design Studio 入门指南:从零开始打造炫酷UI(详细+实用+轻松上手)

本文是Qt Design Studio的详细入门指南,旨在帮助UI设计新手从零开始轻松打造炫酷界面。文章系统介绍了软件的核心概念、安装步骤、主界面解析,并通过创建一个完整的登录界面实例,手把手教学如何拖拽组件、设置属性、添加交互状态与动画。同时,分享了创建自定义组件、管理资源以及提升效率的实用技巧与常见问题解决方案,让读者能够快速上手并应用于实际项目开发。

devops8pract的博客 589

半透明PopupWindow

1.设置半透明主题2.设置window的alpha值//                        WindowManager.LayoutParams lp = getWindow().getAttributes();//            lp.alpha = 0.5

wai2014kuaile的专栏 4734

Vue2 - 最新详细实现接入天地图详细配置流程及示例代码教程,提供获取用户当前定位的城市名称及省市区数据、精准经纬度信息、IP属地定位、地图搜索标点等示例,解决天地图跨域报错/页面白屏/定位不准问题

vue2,nuxt2,天地图,使用教程,vue接入国家天地图,定位api,ip属地定位,获取用户当前位置数据,省市区,地图不显示,白屏,报错,vue2如何使用天地图,地图选点,cors跨域,白名单设置了还是不行,vue项目调用天地图api实现定位,Vue2中使用天地图,详细教程,天地图申请密钥key教程,经纬度,经纬度不准确,一直定位北京,定位在北京不准怎么办,nuxt项目天地图,网站,网页,h5页面,地图显示不全,获取当前定位,如何使用天地图,拿到客户ip地址定位城市名称,距离计算,实时搜索地址逆解析,天

🏆 前端领域优质创作者 5327

ios 设置控制器背景半透明_iOS实现底部弹出PopupWindow效果 iOS改变背景透明效果...

底部弹出PopupWindow,背景变为半透明效果,采用两种方式实现先来看看运行效果图 [方式一]实现从底部弹出PopupWindow原理:定义一个高度为wrap_content的PopupWindow布局文件,根据屏幕底部的位置显示在Bottom1.首先定义一个高度为wrap_content的PopupWindow布局文件android:layout_width="match_parent"an...

weixin_28808145的博客 543

使用 vLLM 和 YaRN 扩展技术部署 Qwen3-32B 大模型:实现 80K 长文本推理

vLLM 是一个高性能的 LLM 推理和服务引擎,以其高效的注意力机制(PagedAttention)和优异的内存管理著称。它提供了与 OpenAI API 兼容的接口,使得现有应用可以无缝迁移。通过vLLM与YaRN技术的结合,我们成功将Qwen3-32B的上下文处理能力从32K扩展到80K,为长文本应用场景提供了强大的技术支持。这种部署方案兼具高效性和实用性,是处理大模型长上下文需求的优选方案。随着硬件和算法的不断进步,我们期待看到更大上下文窗口的模型服务成为可能。延伸阅读vLLM官方文档。

Ven%的博客 1749

刚体转动欧拉方程:从理论到卫星姿态控制的实践

刚体转动欧拉方程是描述刚体旋转运动的基本动力学方程,在卫星姿态控制、机器人学、航空航天等领域有着广泛应用。本文将详细讲解欧拉方程的物理概念、数学推导、数值计算方法,并重点分析其在卫星姿态控制系统中的具体应用。

ScilogyHunter的博客 643

PopupWindow 弹出背景半透明设置方法

需求:PopupWindow 在弹出的时候需要设置背景为半透明状态,写个简单的工具类,方法如下: public class MainActivity extends AppCompatActivity { private PopupWindow mPop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

st526403649的博客 1470

SAP CBC 集成质量系统初始化接入实战指南

摘要 本文探讨了SAP S/4HANA Cloud Public Edition项目中质量系统的初始接入过程。质量系统并非开箱即用,而是需要经过SAP Central Business Configuration(CBC)的Scope and Organizational Structure阶段部署后才能具备完整功能。文章重点分析了几个关键环节:CBC里程碑确认的时机选择、Identity Authentication的初始管理员设置、Business Role Template的创建规范等。实施过程中需特

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 1408

android中popupwindow弹出后,屏幕背景变成半透明

android中popupwindow弹出后,屏幕背景变成半透明这个效果很普通。实现的方法也很多。我使用的可能是最简单的一种,就是设置一下getWindows的透明度。不多说上代码 /** * 设置添加屏幕的背景透明度 * @param bgAlpha */ public void backgroundAlpha(float bgAlpha) { WindowManager.

我本无名的专栏 10万+

鱼鹰优化算法(OOA)详解:从猛禽捕食智能到优化算法实现

鱼鹰优化算法(Osprey Optimization Algorithm, OOA)是2023年提出的一种新型元启发式优化算法,灵感来源于鱼鹰(一种中型猛禽)高效的捕食策略。算法模拟了鱼鹰从空中识别水下鱼类位置并俯冲捕食,然后将捕获的鱼带至安全位置享用的全过程,以此抽象出全局探索和局部开发两个核心阶段。OOA算法具有参数少、结构简单、收敛速度快、全局寻优能力强等特点。自提出以来,已在函数优化、机器学习参数调优(如CatBoost模型)、路径规划等多个领域展现出良好性能,为解决复杂优化问题提供了新的有效工具。

qq_42212808的博客 273

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

popupwindow 底部弹出+背景半透明

首先是布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/popup_linear" android:layout_width="match_parent" andr

qq_33903211的博客 1698

蓝牙模块HM-10手册

蓝牙模块HM-10手册,该手册对蓝牙模块HM-10进行了详细的介绍和使用方法以及一些AT指令。

PopupWindow弹出使背景变暗的问题

今天有个需求是要在点击操作是,从屏幕下面弹出一个如下的选择框: 首先想到的就是使用popuwindow来实现 做起来非常简单 ViewcontentView=View.inflate(mContext,R.layout.popupwindow,null); ...

chijizhong9840的博客 433

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

android 列表底部弹出框,Android 底部弹出提示框的解决办法(使用Activity以及PopupWindow)...

本片文章主要谈探讨了如何实现在底部弹出提示框背景为半透明效果的实现。想要实现此种效果一般有两种方式一个是使用Activity设置Theme另一种方式就是使用PopupWindow设置样式实现效果。一,使用Activity首先是此activity的布局文件:xmlns:android="http://schemas.android.com/apk/res/android"android:layout...

weixin_28951059的博客 1121

PopupWindow弹出窗体

PopupWindow 一.PopupWindow介绍 二.如何自定义窗体 三.实现点击右上角弹出窗体 四.底部弹出窗体 五.弹出窗体背景半透明 六.设置动画 1.在res/anim文件夹下定义进场动画 2.定义进出场动画 3.为popupwindow设置动画 七.自定义PopupWindow PopupWindow 显示方法 显示位置 showAsDropDown(View an...

liuguitong_BK的博客 290

popupwindow在顶部或底部出现,背景变暗

popupwindow在顶部或底部出现,背景变暗

u014620028的专栏 3634

第2天PopupWindow弹出窗体

第2天PopupWindow弹出窗体PopupWindow一.PopupWindow介绍二.如何自定义窗体三.实现微信QQ支付宝右上角加号弹出窗体四.底部弹出窗体五.弹出窗体背景半透明六.设置动画1.在res/anim文件夹下定义进场动画2.定义进出场动画3.为popupwindow设置动画七.自定义PopupWindow PopupWindow 显示方法 显示位置 showAsDropDown(View anchor, int xoff, int yoff) 显示在anchor控件的下方

qq_34178710的博客 692

android PopupWindow实现从底部弹出或滑出选择菜单或窗口

http://www.jb51.net/article/32031.htm http://www.jb51.net/article/32031.htm http://www.jb51.net/article/32031.htm http://www.jb51.net/article/32031.htm http://www.jb51.net/article/32031.ht

1096

popupWindow半透明背景

最近在写关于弹出框需求,特此记录下popupwindow背景透明 网上有关于使用getWindow().getAttributes()设置背景透明,我个人倾向使用setBackgroundDrawable来设置背景头透明。直接上代码 dialog布局 &amp;amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;amp;gt; &amp

weixin_39178206的博客 465

实现从底部弹出的PopupWindow

根据项目需求了一个从底部弹出的PopupWindow,它的作用是选择地址的。省份,市,区都在同一个里面,同时选择。先上效果图。 一、思路1、用一个ListView加载这三个部分的数据,在切换的时候,直接切换数据选择。 2、选完某个地址时,对外提供一个回调事件,比如在省份选择后,则应该查询对应的城市,这时候是要对外提供接口,查询到城市的数据后,再切换到城市的选择。 3、一个从底部弹出的popup

LHavoc的博客 1017
上一篇: android 主线程与子线程通信
下一篇: android viewPager 轮播图
xy%
xy%
博客等级 码龄11年 10粉丝 27原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值