Android 的PopupWindow的简单设置及空白区域颜色

直接上代码

//设置空白的背景色
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.alpha = 0.3f;
getActivity().getWindow().setAttributes(lp);
// 用于PopupWindow的View
View contentView = LayoutInflater.from(getContext()).inflate(R.layout.quote_info_pop, null, false);
// 创建PopupWindow对象,其中:
// 第一个参数是用于PopupWindow中的View,第二个参数是PopupWindow的宽度,
// 第三个参数是PopupWindow的高度,第四个参数指定PopupWindow能否获得焦点
PopupWindow window = new PopupWindow(contentView, getScreenWith() / 3 * 2, (int) getScreenHeight() / 3 * 2, true);
// 设置PopupWindow的背景
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.white_shap));
// 设置PopupWindow是否能响应外部点击事件
window.setOutsideTouchable(true);
// 设置PopupWindow是否能响应点击事件
window.setTouchable(true);
// 显示PopupWindow,其中:
// 第一个参数是PopupWindow的锚点,第二和第三个参数分别是PopupWindow相对锚点的x、y偏移
listView = contentView.findViewById(R.id.list);
quoteInfoAdapter = new QuoteInfoAdapter(getActivity(), mList);
listView.setAdapter(quoteInfoAdapter);
window.showAsDropDown(v, getScreenWith() / 6, 0);
// 或者也可以调用此方法显示PopupWindow,其中:
// 第一个参数是PopupWindow的父View,第二个参数是PopupWindow相对父View的位置,
// 第三和第四个参数分别是PopupWindow相对父View的x、y偏移
// window.showAtLocation(parent, gravity, x, y);
//添加pop窗口关闭事件
window.setOnDismissListener(new poponDismissListener());

还要加上监听,否则,pop消失时,透明度不会变回来

class poponDismissListener implements PopupWindow.OnDismissListener {

    @Override
    public void onDismiss() {
        // TODO Auto-generated method stub
        //Log.v("List_noteTypeActivity:", "我是关闭事件");
        WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
        lp.alpha = 1f; //0.0-1.0
        getActivity().getWindow().setAttributes(lp);

    }
}

猜你喜欢

转载自blog.csdn.net/qq_30711091/article/details/81076237