1.在layout里新建一popwindow.xml
2.在Activity里
//popwindow part
private ImageButton popButton;
private boolean click = true;
private PopupWindow popUp;
private View layout;
final View poplayout = this.getLayoutInflater().inflate(R.layout.popwindow, null);
popUp = new PopupWindow(poplayout,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
layout = getLayoutInflater().inflate(R.layout.activity_my_fax_main_page,null);
//获得layout
//点击窗口外消失
popUp.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
popUp.setOutsideTouchable(true);
popUp.setFocusable(true);
popButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popUp.showAtLocation(layout, Gravity.BOTTOM, 0, 0);
popUp.showAsDropDown(layout); //设置弹出效果
popUp.showAsDropDown(null, 0, layout.getHeight())
//窗口内点击
poplayout.findViewById(R.id.footer_button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyFaxMainPage.this, "clicked", Toast.LENGTH_SHORT).show();
popUp.dismiss();
}
});
poplayout.findViewById(R.id.footer_button2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MyFaxMainPage.this, "clicked", Toast.LENGTH_SHORT).show();
popUp.dismiss();
}
});
}
});
因为按钮在窗口内,所以需要先获取窗口布局(非popupwindow,是布局XML),然后在布局内findviewbyid获取按钮,最后设置点击事件
效果如下: