PopupWindow屏幕中部显示背景变暗

在学习的过后和大家分享一下小知识点。PopupWindow弹出框中部显示并且背景变暗


直接看代码吧,很简单!

//创建PopupWindow布局
if(pop_camera==null){
    pop_camera = CommonUtils.inflate(R.layout.pop_camera_layout);
    take_picture = (TextView) pop_camera.findViewById(R.id.take_picture);
    photo_album = (TextView) pop_camera.findViewById(R.id.photo_album);
}
//创建PopupWindow,宽高为包裹内容
popupWindow = new PopupWindow(pop_camera, LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);
//获得焦点
popupWindow.setFocusable(true);
//设置外部可点击效果
popupWindow.setOutsideTouchable(true);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
//PopupWindow显示在屏幕中央
popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
//弹出是背景变暗
backgroundAlpha(100);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
    @Override
    public void onDismiss() {
        //消失后背景变浅
        backgroundAlpha(10);
    }
});

/**
 * 背景变暗
 * @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);
}

就这样就完全Ok了


阅读更多
个人分类: PopupWindow
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭