码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-01-12 19:22:18      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:android   alertdialog   popupwindow   windowmanager   activity   


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

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);

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

标签:android   alertdialog   popupwindow   windowmanager   activity   

原文地址:http://blog.csdn.net/figo0423/article/details/42645281

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!