一个类似淘宝弹出框的示例

效果如图





代码如下:

Button show;
RelativeLayout main;
private PopupWindow mPopupWindow;
private View mView;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    show = (Button) findViewById(R.id.show);
    main = (RelativeLayout) findViewById(R.id.main);

    //这个是你弹出框需要的布局
    mView = View.inflate(MainActivity.this, R.layout.example_pop, null);
   
    Button button = (Button) mView.findViewById(R.id.button_show);
    
    //这个是实验弹出框的点击事件是否可以响应
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this, "出现吧", Toast.LENGTH_SHORT).show();

        }
    });
    
    mPopupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    mPopupWindow.setFocusable(true);
    mPopupWindow.setOutsideTouchable(true);
    mPopupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null));
    mPopupWindow.getContentView().setFocusableInTouchMode(true);
    mPopupWindow.getContentView().setFocusable(true);




    mPopupWindow.getContentView().setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_MENU && event.getRepeatCount() == 0
                    && event.getAction() == KeyEvent.ACTION_DOWN) {
                if (mPopupWindow != null && mPopupWindow.isShowing()) {

                    mPopupWindow.dismiss();

                }
                return true;
            }
            return false;
        }
    });
    mPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
        @Override
        public void onDismiss() {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0.5f, 1.0f);

            alphaAnimation.setFillAfter(true);
            main.startAnimation(alphaAnimation);
        }
    });

    show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.5f);

            alphaAnimation.setFillAfter(true);
            main.startAnimation(alphaAnimation);
            mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        }
    });
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值