PopWindows背景半透明设置
来源:互联网 发布:淘宝嘉定仓库 编辑:程序博客网 时间:2024/05/20 08:02
PopupWindow可以实现跟Dialog一样的效果,但相对于Dialog,PopupWindow可以设置任意的显示位置,其中有
1、showAtLocation()显示在指定位置,有两个方法重载:
public void showAtLocation(View parent,int gravity,int x,int y)
public void showAtLocation(View parent,int gravity,int x,int y)
这里我们可以根据具体的位置计算弹出窗口的位置
2、showAsDropDown()显示在一个参照物View的周围,有三个方法重载:
public void showAsDropDown(View anchor) 默认显示在参照物View的下方
public void showAsDropDown(View anchor, int xoff, int yoff)可以设置相应的偏移量显示
public void showAsDropDown(View anchor, int xoff, int yoff, int gravity)相对位置中设置gravity
PopupWindow设置外部点击消失必须设置
.setOutsideTouchable(true);
.setBackgroundDrawable(Drawable background);
二者缺一不可,否则没有效果。
代码如下:
View view = LayoutInflater.from(getActivity()).inflate(R.layout.warn_coach_dialog, null);
final PopupWindow popupWindow = new PopupWindow(view, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//测量view 注意这里,如果没有测量 ,下面的popupHeight高度为-2 ,因为LinearLayout.LayoutParams.WRAP_CONTENT这句自适应造成的
view.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
// 允许点击外部消失
popupWindow.setBackgroundDrawable(new BitmapDrawable());//注意这里如果不设置,下面的setOutsideTouchable(true);允许点击外部消失会失效
popupWindow.setOutsideTouchable(true); //设置外部点击关闭ppw窗口
popupWindow.setFocusable(true);
// 设置弹出后背景半透明
backgroundAlpha(0.5f);
popupWindow.showAsDropDown(regCoachWarnIcon, 0, 0);
调用pop的消失的监听 OnDismissListener() 消失后设置背景为全透明
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1f);
}
});
/**
* 设置添加屏幕的背景透明度
*
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getActivity().getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
getActivity().getWindow().setAttributes(lp);
}
其中 .getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);这行代码是为了解决 华为手机 pop弹出后屏幕背景没有变化的方法,这样就可以是实现如同dialog弹出时一样的效果。
二、扩展
其次还可以设置弹出的动画效果
popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style);
在style中设置弹出样式
<@color/colorPrimary
@color/colorPrimaryDark
@color/colorAccent
@anim/in
@anim/out
在anim中设置弹出和消失的动画,具体想要实现什么样的效果,可以根据需求具体是实现。
另外,这里有篇较好的解释PopupWindow的文章 具体链接:http://www.cnblogs.com/mengdd/p/3569127.html
- PopWindows背景半透明设置
- div设置背景半透明
- tableview 背景设置为半透明
- 背景半透明CSS兼容设置
- sublime text 背景半透明设置
- 设置button背景的半透明效果!!!
- Android Activity背景半透明设置方法
- 关于设置半透明背景的一些技巧
- html通过css来设置半透明背景
- Android Alertdialog弹出框设置半透明背景
- Android背景设置透明和半透明效果
- android activity设置透明或者半透明背景
- 背景半透明
- popwindows
- popwindows
- popWindows
- android 设置Button或者ImageButton的背景透明 半透明
- Qt 的一些心得(背景设置,圆角,半透明)
- 数据仓库--数据仓库与数据集市建模
- tp3.2中企业向个人微信钱包零钱付款
- LeetCode——595. Big Countries(mysql)
- greendao的简单使用1
- Redis 数据类型详解及 应用环境
- PopWindows背景半透明设置
- 利用差分实现的树状数组区间修改 区间求和
- SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
- Error:A problem was found with the configuration of task ':app:packageBAIDURelease'.
- 集合(3)把一个集合插入到另一个集合中
- Android之获取证书文件(pfx格式)的公钥和私钥
- 单项链表之创建
- Android 代码实现viewPager+fragment 模仿今日头条的顶部导航
- 【client_ip】通过v$session查询客户端的IP信息