android--------Popupwindow的使用

PopupWindow在Android.widget包下,项目中经常会使用到PopupWindow做菜单选项,  PopupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。

 

效果图:

 

 

MainActivity.java

 

 1 public class MainActivity extends Activityimplements OnClickListener {
 2  
 3          private PopupWindowm PopWindow;
 4          private View parentView;
 5          private Context context;
 6          ImageView mImageViewBar;
 7  
 8          @Override
 9          protected void onCreate(Bundle savedInstanceState) {
10                    super.onCreate(savedInstanceState);
11                    requestWindowFeature(Window.FEATURE_NO_TITLE);
12                    setContentView(R.layout.activity_main);
13                    context = this;
14                    initView();
15       }
16  
17       private void initView() {
18         // TODOAuto-generated method stub
19           parentView =getLayoutInflater().inflate(R.layout.activity_main,null);
20           mImageViewBar =(ImageView) findViewById(R.id.menu);
21           mImageViewBar.setOnClickListener(this);
22           showPopupWindow();
23      }
24  
25       private void showPopupWindow() {
26            LayoutInflater inflater = (LayoutInflater)context
27             .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
28           View contentView =inflater.inflate(R.layout.popuplayout,null);
29           mPopWindow = newPopupWindow(contentView);
30           mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
31           mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
32           mPopWindow = newPopupWindow();
33           // 设置SelectPicPopupWindow的View
34           mPopWindow.setContentView(contentView);
35           // 设置SelectPicPopupWindow弹出窗体的宽
36           mPopWindow.setWidth(LayoutParams.WRAP_CONTENT);
37           // 设置SelectPicPopupWindow弹出窗体的高
38           mPopWindow.setHeight(LayoutParams.WRAP_CONTENT);
39           // 设置SelectPicPopupWindow弹出窗体可点击
40          mPopWindow.setFocusable(true);
41          mPopWindow.setOutsideTouchable(true);
42          // 刷新状态
43          mPopWindow.update();
44          // 实例化一个ColorDrawable颜色为半透明
45          ColorDrawable dw = new ColorDrawable(0000000000);
46          // 点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener,设置其他控件变化等操作
47          mPopWindow.setBackgroundDrawable(dw);
48          contentView.findViewById(R.id.memu1).setOnClickListener(this);
49          contentView.findViewById(R.id.memu2).setOnClickListener(this);
50          contentView.findViewById(R.id.memu3).setOnClickListener(this);
51       }
52  
53        @Override
54       public void onClick(View v) {
55          int id =v.getId();
56          switch (id) {
57            case R.id.menu:
58                  mPopWindow.showAsDropDown(mImageViewBar);
59                  break;
60            case R.id.memu1: {
61                  Toast.makeText(context,"消息", Toast.LENGTH_SHORT).show();
62                  mPopWindow.dismiss();
63                }
64                 break;
65            case R.id.memu2: {
66                 Toast.makeText(context,"收藏", Toast.LENGTH_SHORT).show();
67                 mPopWindow.dismiss();
68               }
69                break;
70            case R.id.memu3: {
71                 Toast.makeText(context,"首页", Toast.LENGTH_SHORT).show();
72                 mPopWindow.dismiss();
73               }
74                break;
75              }
76       }
77   
78       @Override
79        protected void onDestroy() {
80            // TODOAuto-generated method stub
81             super.onDestroy();
82            mPopWindow.dismiss();
83      }
84   }

 

 

 

 

 源代码下载

 

 

 

posted @ 2016-11-24 17:13  切切歆语  阅读(399)  评论(0编辑  收藏  举报