首页 > 代码库 > Popupwindow的使用

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   }

 

activity_main.xml
<?
xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:orientation="vertical"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#E4E4E4" android:minHeight="55dp"> <ImageView android:layout_width="20dp" android:layout_height="20dp" android:layout_centerVertical="true" android:layout_marginLeft="8dp" android:layout_marginRight="5dp" android:focusable="true" android:src="@drawable/abc_ic_ab_back_mtrl_am_alpha"/> <ImageView android:id="@+id/menu" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="9dp" android:src="@drawable/top_arrow_message1"/> </RelativeLayout> </LinearLayout>

popuplayout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:orientation="vertical"> <LinearLayout android:layout_width="120dp" android:layout_height="130dp" android:layout_marginRight="2dp" android:background="@drawable/danchu" android:gravity="center_vertical" android:orientation="vertical"> <RelativeLayout android:id="@+id/memu1" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_marginTop="5dp" android:gravity="center_vertical"> <ImageView android:id="@+id/xiaoxi" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:src="@drawable/android_arrow_application"/> <TextView android:id="@+id/pop_computer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/xiaoxi" android:text="消息" android:textColor="#FFFFFF"/> </RelativeLayout> <View android:layout_width="wrap_content" android:layout_height="1dp"/> <RelativeLayout android:id="@+id/memu2" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_marginTop="5dp" android:gravity="center_vertical"> <ImageView android:id="@+id/shanchu" android:layout_width="20dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:src="@drawable/android_arrow_profitbillsm"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/shanchu" android:text="收藏" android:textColor="#FFFFFF"/> </RelativeLayout> <View android:layout_width="wrap_content" android:layout_height="1dp"/> <RelativeLayout android:id="@+id/memu3" android:layout_width="wrap_content" android:layout_height="30dp" android:layout_marginBottom="5dp" android:layout_marginTop="5dp" android:gravity="center_vertical"> <ImageView android:id="@+id/dianjifanhui" android:layout_width="18dp" android:layout_height="20dp" android:layout_marginLeft="10dp" android:src="@drawable/dir15"/> <TextView android:id="@+id/pop_manage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_toRightOf="@+id/dianjifanhui" android:text="首页" android:textColor="#FFFFFF"/> </RelativeLayout> <View android:layout_width="wrap_content" android:layout_height="1dp"/> </LinearLayout> </LinearLayout>

 

 

项目源码:

Eclipse: http://download.csdn.net/detail/dickyqie/9615658

AndroidStudio: https://github.com/DickyQie/PopupWindow

 

 

Popupwindow的使用