android开发步步为营之51:弹出窗及遮罩层的几种实现方式


需要做一个弹出窗或者遮罩层,我们一般有以下几种思路。

1、AlertDialog对话框

2、PopupWindow弹出窗

3、WindowManager动态添加View到当前页面

4、打开另外一个Activity

 

下面分别给出这几种方法的实现栗子。

一、AlertDialog

适合需要用户做出选择,或者确认的弹出小窗

 

                AlertDialog.Builder dialog = new AlertDialog.Builder(TestActivity.this);

                //自定义

                //dialog.setView(LayoutInflater.from(TestActivity.this).inflate(R.layout.layout_dialog, null));

                dialog.setTitle("Do you like Android?");

                dialog.setNegativeButton("No"new DialogInterface.OnClickListener() {

 

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        dialog.dismiss();

                    }

 

                });

                dialog.setPositiveButton("Yes"new DialogInterface.OnClickListener() {

 

                    @Override

                    public void onClick(DialogInterface dialog, int which) {

                        Toast.makeText(TestActivity.this"you choose yes", Toast.LENGTH_SHORT).show();

                        dialog.dismiss();

                    }

                });

                dialog.show();

二、PopupWindow

适合做遮罩页面

 

     final PopupWindow popup = new PopupWindow(TestActivity.this);

                View popView = LayoutInflater.from(TestActivity.this).inflate(R.layout.popuptestnull);

                popup.setContentView(popView);

                popup.setFocusable(true);

                DisplayMetrics metric = new DisplayMetrics();

                getWindowManager().getDefaultDisplay().getMetrics(metric);

                int width = metric.widthPixels// 屏幕宽度(像素)

                int height = metric.heightPixels// 屏幕高度(像素)

                popup.setWidth(Integer.parseInt(new DecimalFormat("0").format(width * 0.8)));

                popup.setHeight(Integer.parseInt(new DecimalFormat("0").format(height * 0.8)));

                popup.showAtLocation(TestActivity.this.findViewById(R.id.btnDialog), Gravity.CENTER, 0, 0);

                popView.setOnClickListener(new OnClickListener() {

                    @Override

                    public void onClick(View v) {

                        popup.dismiss();

                    }

              });

三、WindowManager

适合弹出带动画的小窗

 //WindowManager

                View vRating; 

                try {

                   vRating = LayoutInflater.from(TestActivity.this).inflate(R.layout.layout_rating, null);

                } catch (Exception e) {

                    return;

                } catch (OutOfMemoryError e) {

                    return;

                }

                //设置页面属性

                WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();

                layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT;

                layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT;

                layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

                        | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;

                //        layoutParams.flags = WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN

                //                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS| WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;

                layoutParams.format = PixelFormat.TRANSLUCENT;

                //        layoutParams.type = WindowManager.LayoutParams.TYPE_TOAST;

                layoutParams.type = WindowManager.LayoutParams.TYPE_PHONE;

                layoutParams.x = 0;

                layoutParams.y = 0;

 

                final WindowManager wm = (WindowManager) TestActivity.this.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

                wm.addView(vRating, layoutParams);

                final View negativeButton = vRating.findViewById(R.id.negativeButton);

                negativeButton.setOnClickListener(new OnClickListener() {

                    //to do

                    });

                }

                final View positiveButton = vRating.findViewById(R.id.positiveButton);

                positiveButton.setOnClickListener(new OnClickListener() {

                    //to do

                });

四、Activity

适合做透明遮罩页面,实现原理是跳转到另外一个Activity  A后,再延时跳出一个Activity B,这样后面的B就会遮住前面的A

  //Activity

                final Handler handler = new Handler();

                Runnable runnable = new Runnable() {

                    @Override

                    public void run() {

                        Intent intent = new Intent(TestActivity.this, DialogActivity.class);

                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        TestActivity.this.startActivity(intent);

                    }

                };

                handler.postDelayed(runnable, 500L);

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。