码迷,mamicode.com
首页 > Windows程序 > 详细

自定义的PopUpWindow

时间:2015-07-21 11:56:30      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:

 1 package com.example.widget;
 2 
 3 import android.app.Activity;
 4 import android.content.Context;
 5 import android.graphics.drawable.ColorDrawable;
 6 import android.view.Gravity;
 7 import android.view.LayoutInflater;
 8 import android.view.View;
 9 import android.view.View.OnClickListener;
10 import android.view.ViewGroup.LayoutParams;
11 import android.view.WindowManager;
12 import android.widget.PopupWindow;
13 import android.widget.TextView;
14 
15 import com.example.home.R;
16 
17 public class PickPicPopWindow extends PopupWindow {
18 
19     private View view;
20     private TextView tv_pickPic_album;
21     private TextView tv_pickPic_camera;
22     private Activity activity;
23     
24     public PickPicPopWindow(Activity context,OnClickListener itemsOnClick) {
25         // TODO Auto-generated constructor stub
26         super(context);
27         LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
28         view = inflater.inflate(R.layout.pickpic_popwindow, null);
29         
30         initPopUpWindow();
31         
32         tv_pickPic_album = (TextView)view.findViewById(R.id.pickpic_tv_album);
33         tv_pickPic_camera = (TextView)view.findViewById(R.id.pickpic_tv_camera);
34         
35         tv_pickPic_camera.setOnClickListener(itemsOnClick);
36         tv_pickPic_album.setOnClickListener(itemsOnClick);
37         this.activity = context;
38         
39         setBackgroundGray(activity);
40     }
41     
42     //背景变灰
43     public void setBackgroundGray(Activity activity)
44     {
45         WindowManager.LayoutParams lp=activity.getWindow().getAttributes();
46         lp.alpha = 0.7f;
47         activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
48         activity.getWindow().setAttributes(lp);
49     }
50     
51     //清楚背景变灰
52     public void clearBackgroundGray(Activity activity)
53     {
54         WindowManager.LayoutParams lp=activity.getWindow().getAttributes();
55         lp.alpha = 1.0f;
56         activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
57         activity.getWindow().setAttributes(lp);
58     }
59     
60     public void initPopUpWindow()
61     {
62         this.setOutsideTouchable(true);
63         
64         this.setContentView(view);
65         this.setWidth(LayoutParams.FILL_PARENT);
66         this.setHeight(LayoutParams.WRAP_CONTENT);
67         this.setFocusable(true);
68         
69         ColorDrawable dw = new ColorDrawable(0xffffffff);
70         this.setBackgroundDrawable(dw);
71         
72         this.setOnDismissListener(new OnDismissListener() {
73             
74             @Override
75             public void onDismiss() {
76                 // TODO Auto-generated method stub
77                 clearBackgroundGray(activity);
78             }
79         });
80     }
81     
82     //popupwindow在某个控件的上面
83     public void showAsPullUp(View view, int x,int y)
84     {
85         int[] location = new int[2];
86         view.getLocationOnScreen(location);
87         
88         this.getContentView().measure(0,0);    //强行绘制
89         int height = this.getContentView().getMeasuredHeight();
90         
91         this.showAtLocation(view, Gravity.NO_GRAVITY, location[0]-x, location[1]-height-y);
92     }
93 
94 }

 

 

注意:

popupWindow 获取高度 出现 -2,0,-1的问题?

 

出现这个的原因就是因为PopupWindow的尺寸拿不到,因为内容的View的width和height都是wrap_content,所以在PopupWindow里面的contentView还没被绘制出来的时候,这两个值都还是0。

如果直接调用PopupWindow的getWidth()和getHeight(),会发现拿到的都是ViewGroup.LayoutParams.WRAP_CONTENT的值 -2;

解决的方法就是在初始化contentView的时候,强制绘制contentView,并且马上初始化contentView的尺寸。

 

这里只需要一句代码即可:

this.getContentView().measure(0,0); //强行绘制
int height = this.getContentView().getMeasuredHeight();

自定义的PopUpWindow

标签:

原文地址:http://www.cnblogs.com/AchillesSnow/p/4663679.html

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