MainActivity:
private PopupWindow popupWindow;
private View popupWindow_view;
private TextView but_back, but_stolen, but_cogradient, but_recover;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupWindow_view = getLayoutInflater().inflate(R.layout.layout_rg_delcheck, null, false);
but_back = (TextView) popupWindow_view.findViewById(R.id.tv_back);
but_back.setOnClickListener(this);
but_stolen = (TextView) popupWindow_view.findViewById(R.id.tv_stolen);
but_stolen.setOnClickListener(this);
but_cogradient = (TextView) popupWindow_view.findViewById(R.id.tv_cogradient);
but_cogradient.setOnClickListener(this);
but_recover = (TextView) popupWindow_view.findViewById(R.id.tv_recover);
but_recover.setOnClickListener(this);
findViewById(R.id.imagr_view).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPopupWindow(v);
}
});
}
public void showPopupWindow(View view) {
// 創(chuàng)建PopupWindow實(shí)例,300,LayoutParams.MATCH_PARENT分別是寬度和高度
popupWindow = new PopupWindow(popupWindow_view, 300, ViewGroup.LayoutParams.MATCH_PARENT, true);
// 如果不設(shè)置PopupWindow的背景,無(wú)論是點(diǎn)擊外部區(qū)域還是Back鍵都無(wú)法dismiss彈框(必須設(shè)置)
popupWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.mainmenubottom1));
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.anim.anim_pop); //設(shè)置加載動(dòng)畫(huà)
popupWindow.setTouchable(true);
popupWindow.setFocusable(true);
//設(shè)置popupWindow顯示的位置措左,參數(shù)依次是參照View辙培,x軸的偏移量拾积,y軸的偏移量
popupWindow.showAsDropDown(view, 0, 15);
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 0.7f;
getWindow().setAttributes(lp);
// 彈出菜單欄背景變暗
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = 1f;
getWindow().setAttributes(lp);
}
});
}
動(dòng)畫(huà)文件:anim_pop.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0"
android:toAlpha="1"
android:duration="2000">
</alpha>
</set>