Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)
来源:互联网 发布:软件系统分类 编辑:程序博客网 时间:2024/04/29 15:21
最近尝试使用popupWindow实现背景变暗效果,自己优化了一下,并封装成一个可以调用的方法,默认实现弹出窗口显示在传入view的下方,以下代码有详细注释,有问题可以留言
展示效果如下:
代码展示
布局中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="246dp" android:layout_height="132dp" android:layout_gravity="center" android:gravity="center" > <TextView android:id="@+id/tv_pop_web" android:layout_width="wrap_content" android:layout_height="32dp" android:background="@color/nba_white" android:clickable="true" android:drawableLeft="@drawable/icon_pop_web" android:drawablePadding="11dp" android:gravity="center_vertical" android:text="观看网页版" android:textColor="@color/nba_black" android:textSize="16dp" /> <TextView android:id="@+id/tv_pop_phone" android:layout_width="wrap_content" android:layout_height="32dp" android:layout_below="@+id/tv_pop_web" android:layout_marginTop="16dp" android:background="@color/nba_white" android:clickable="true" android:drawableLeft="@drawable/icon_pop_phone" android:drawablePadding="11dp" android:gravity="center_vertical" android:text="打开腾讯体育观看" android:textColor="@color/nba_black" android:textSize="16dp" /></RelativeLayout>
/* * 弹出选择直播方式的弹框 * View v :显示在那个父view内 * int convertViewResource :要填充到popupWindow中的布局文件id * int drawbelResource :int drawbelResource * */ private void showPopWindow(View parentView, int convertViewResource, int drawbelResource, String matchId) { //创建一个popUpWindow View popLayout = LayoutInflater.from(this).inflate(convertViewResource, null); //给popUpWindow内的空间设置点击事件 popLayout.findViewById(R.id.tv_pop_web).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } loadHtml(mQqSportWebUrl); } }); popLayout.findViewById(R.id.tv_pop_phone).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } loadHtml(String.format(Api.SKIP_TO_QQSPORT_LIVE_VIDEO, matchId)); } }); if (mPopupWindow == null) { //实例化一个popupWindow mPopupWindow = new PopupWindow(popLayout, DensityUtil.dp2px(this, 246), DensityUtil.dp2px(this, 132)); //产生背景变暗效果 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp); //点击外面popupWindow消失 mPopupWindow.setOutsideTouchable(true); //popupWindow获取焦点 mPopupWindow.setFocusable(true); //popupWindow设置背景图 Drawable drawable = getResources().getDrawable(drawbelResource); mPopupWindow.setBackgroundDrawable(drawable); //popupWindow设置开场动画风格 //popupWindow.setAnimationStyle(R.style.popupWindow_anim); //刷新popupWindow //popupWindow.update(); //设置popupWindow消失时的监听 mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() { //在dismiss中恢复透明度 public void onDismiss() { WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } }); mPopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0); } else { //如果popupWindow正在显示,接下来隐藏 if (mPopupWindow.isShowing()) { mPopupWindow.dismiss(); } else { //产生背景变暗效果 WindowManager.LayoutParams lp = getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp); mPopupWindow.showAtLocation(parentView, Gravity.CENTER, 0, 0); } } }
2 0
- Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)
- Android实现底部弹出PopupWindow背景逐渐变暗效果
- 弹出PopupWindow背景变暗的实现
- PopupWindow弹出框与背景变暗的实现(附带动画效果)
- Android-实现底部弹出PopupWindow并让背景逐渐变暗
- PopupWindow弹出后其他地方变暗的效果如何实现
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)!!!!
- Android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- android弹出窗口的实现(PopupWindow)
- Android弹出窗口的实现(PopupWindow)
- Android 动画效果弹出PopupWindow,同时屏幕背景变暗
- Activity生命周期图
- Spark搭建HA详解
- REST初体验
- webview修改版式
- js--面向对象--非构造函数的继承
- Android PopupWindow弹出窗口的完美实现(实现弹出背景变暗效果)
- 【LeetCode】Increasing Triplet Subsequence
- 计算机视觉与机器视觉
- Eclipse-----Eclipse中SVN过滤提交
- CC2530+74HC164矩阵键盘的设计
- [C#]浮雕化、锐化图像处理
- pwntools使用简介
- <Mac OS X中开启或关闭隐藏文件夹> And <安装了两个版本的Xcode,终端下Git出错>
- CY7c68013 Venor命令学习笔记