Android开发之自定义PopupWindow记录

1、继承PopupWindow :
package com.qy.foreign_trade.dialog;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;

import com.qy.foreign_trade.R;

/**
 * Created by xu_xin on 2019/7/3 0003.
 */
public class XPopupWindow1 extends PopupWindow {
    private WindowManager.LayoutParams lp;
    private Activity mContext;
    private View contentView;

    public XPopupWindow1(Context context) {
        mContext = (Activity) context;
        lp = mContext.getWindow().getAttributes();
        setContentView(init());
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                setAlpha(1f);
            }
        });
    }

    public View getContentView() {
        return contentView;
    }

    private View init() {
        contentView = View.inflate(mContext, R.layout.pop_tips, null);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setBackgroundDrawable(new ColorDrawable(0x00FFFFFF));
        setFocusable(false);
        setOutsideTouchable(false);
        return contentView;
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        setAlpha(0.7f);
        super.showAtLocation(parent, gravity, x, y);
    }

    @Override
    public void showAsDropDown(View anchor) {
        setAlpha(0.7f);
        super.showAsDropDown(anchor);
    }

    private void setAlpha(float alpha) {
        lp.alpha = alpha;
        mContext.getWindow().setAttributes(lp);
    }
}

2、使用

private void sureBack() {
popupWindow = new XPopupWindow1(this.mContext);
View contentView = popupWindow.getContentView();
popupWindow.showAtLocation(contentView, Gravity.CENTER, 0, 0);
TextView tv_state_name = contentView.findViewById(R.id.tv_state_name);
TextView tv_tips = contentView.findViewById(R.id.tv_tips);
LinearLayout ll_hs = contentView.findViewById(R.id.ll_hs);
TextView tv_cencel = contentView.findViewById(R.id.tv_cencel);
TextView tv_suer = contentView.findViewById(R.id.tv_suer);
}

完成

你可能感兴趣的:(Activity,Android自定义View,android,Android,PopupWindow,android自定义)