/**
* Created by HAOJI on 2019/8/12.
*/
public class PopupWindow extends Activity {
private RelativeLayout rl_id;
private Button bt01;
private ButtonPopupwindow buttonPopupwindow;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow);
rl_id = findViewById(R.id.rl_id);
bt01 = findViewById(R.id.bt01);
buttonPopupwindow = new ButtonPopupwindow(this);
buttonPopupwindow.setOnDismissListener(new android.widget.PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
backgroundAlpha(1.f);
}
});
bt01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
setPopupwindow();
}
});
}
private void setPopupwindow() {
backgroundAlpha(0.5f);
buttonPopupwindow.showAtLocation(rl_id, Gravity.BOTTOM, 0, 0);
}
public void backgroundAlpha(float bgAlpha) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
}
自定义view
public class ButtonPopupwindow extends PopupWindow {
private Context context;
public ButtonPopupwindow(Context context) {
super(context);
this.context=context;
setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
setOutsideTouchable(true);
setFocusable(true);
setAnimationStyle(R.style.popwin_anim_style);
ColorDrawable dw = new ColorDrawable(0xb0000000);
setBackgroundDrawable(dw);
View contentView = LayoutInflater.from(context).inflate(R.layout.popup_test,
null, false);
setContentView(contentView);
}
}