其他分享
首页 > 其他分享> > popUpWindow

popUpWindow

作者:互联网

1:点击控件弹出popUpwindow
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        setPopupWindow();
    }
});
2:popUpwindow

/**
 * popupWindow
 */
protected void setPopupWindow() {
    // 一个自定义的布局,作为显示的内容
    View contentView = LayoutInflater.from(this).inflate(
            R.layout.popup_listview, null);
    ConstraintLayout popViewBg = (ConstraintLayout) contentView.findViewById(R.id.pop_view_bg);
    int windowPos[] = ScreenUtils.calculatePopWindowPos(popView, contentView);
    int gravity = Gravity.TOP | Gravity.START;
    int xLocation = windowPos[0];
    int yLocation = windowPos[1];
    popWindowType = popType;
    PopupWindow popupWindow = new PopupWindow(contentView,
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
    popupWindow.setContentView(contentView);
    popupWindow.setFocusable(true);
    popupWindow.setOutsideTouchable(true);
    // 如果不设置PopupWindow的背景,无论是点击外部区域还是Back键都无法dismiss弹框
    popupWindow.setBackgroundDrawable(getResources().getDrawable(
            R.color.transparent));
    //弹出时背景设置未半透黑
    backgroundAlpha(popType, 0.5f);
    // 弹出窗口加载数据
    initPopData(contentView);
    popupWindow.showAtLocation(popView, gravity, xLocation, yLocation);
    popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            //弹窗消失时背景恢复
            backgroundAlpha(popType, 1f);
        }
    });
}

3:弹窗消失时背景恢复

/**
 * 设置添加屏幕的背景透明度
 *
 * @param bgAlpha
 */
public void backgroundAlpha(int popType, float bgAlpha) {
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    if (popType == Content.POP_SET_IN_LOGIN) {
        lp.alpha = bgAlpha; //0.0-1.0
        getWindow().setAttributes(lp);
    }
}

4:初始化并适配弹出列表

/**
 * 初始化popupData
 */
private void initPopData(View defaultView) {
    ListView set_list = (ListView) defaultView.findViewById(R.id.set_list);
    mContext = this;
    mData = getData(); //获取PopupWindow中的listview数据
    SetListViewAdapter adapter = new SetListViewAdapter();
    set_list.setAdapter(adapter);// 给activity中的listview设置数据
    set_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //弹出内容
            }
    });
}


标签:popUpWindow,contentView,popupWindow,int,void,popType,new
来源: https://blog.csdn.net/voipure/article/details/120787997