手记

Popupwindow的使用

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

MainActivity.java

[代码]java代码:

?

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

public class MainActivity   extends Activityimplements   OnClickListener { 

    

         private PopupWindow mPopWindow; 

         private View parentView; 

         private Context context; 

         ImageView   mImageViewBar; 

    

         @Override 

         protected voidonCreate(Bundle savedInstanceState)   { 

                   super.onCreate(savedInstanceState);   

                   requestWindowFeature(Window.FEATURE_NO_TITLE);   

                   setContentView(R.layout.activity_main);   

                   context   = this; 

                   initView();   

      }   

    

      private void initView() { 

        //   TODOAuto-generated method stub 

          parentView   =getLayoutInflater().inflate(R.layout.activity_main,null); 

          mImageViewBar   =(ImageView) findViewById(R.id.menu); 

          mImageViewBar.setOnClickListener(this);   

          showPopupWindow();   

     } 

    

      private void showPopupWindow() { 

           LayoutInflater   inflater = (LayoutInflater)context 

            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);   

          View   contentView =inflater.inflate(R.layout.popuplayout,null); 

          mPopWindow   = newPopupWindow(contentView); 

          mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);   

          mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);   

          mPopWindow   = newPopupWindow(); 

          //   设置SelectPicPopupWindow的View 

          mPopWindow.setContentView(contentView);   

          //   设置SelectPicPopupWindow弹出窗体的宽 

          mPopWindow.setWidth(LayoutParams.WRAP_CONTENT);   

          //   设置SelectPicPopupWindow弹出窗体的高 

          mPopWindow.setHeight(LayoutParams.WRAP_CONTENT);   

          //   设置SelectPicPopupWindow弹出窗体可点击 

         mPopWindow.setFocusable(true);   

         mPopWindow.setOutsideTouchable(true);   

         //   刷新状态   

         mPopWindow.update();   

         //   实例化一个ColorDrawable颜色为半透明 

         ColorDrawable   dw = new ColorDrawable(0000000000);   

         //   点back键和其他地方使其消失,设置了这个才能触发OnDismisslistener,设置其他控件变化等操作 

         mPopWindow.setBackgroundDrawable(dw);   

         contentView.findViewById(R.id.memu1).setOnClickListener(this);   

         contentView.findViewById(R.id.memu2).setOnClickListener(this);   

         contentView.findViewById(R.id.memu3).setOnClickListener(this);   

      }   

    

       @Override 

      public void onClick(View v) { 

         int id =v.getId(); 

         switch (id) { 

           case R.id.menu: 

                 mPopWindow.showAsDropDown(mImageViewBar);   

                 break;   

           case R.id.memu1: { 

                 Toast.makeText(context,"消息",   Toast.LENGTH_SHORT).show(); 

                 mPopWindow.dismiss();   

               }   

                break;   

           case R.id.memu2: { 

                Toast.makeText(context,"收藏",   Toast.LENGTH_SHORT).show(); 

                mPopWindow.dismiss();   

              }   

               break;   

           case R.id.memu3: { 

                Toast.makeText(context,"首页",   Toast.LENGTH_SHORT).show(); 

                mPopWindow.dismiss();   

              }   

               break;   

             }   

      }   

     

      @Override 

       protected void onDestroy() { 

           //   TODOAuto-generated method stub 

            super.onDestroy();   

           mPopWindow.dismiss();   

     } 

  }

 

 

[代码]xml代码:

?

001

002

003

004

005

006

007

008

009

010

011

012

013

014

015

016

017

018

019

020

021

022

023

024

025

026

027

028

029

030

031

032

033

034

035

036

037

038

039

040

041

042

043

044

045

046

047

048

049

050

051

052

053

054

055

056

057

058

059

060

061

062

063

064

065

066

067

068

069

070

071

072

073

074

075

076

077

078

079

080

081

082

083

084

085

086

087

088

089

090

091

092

093

094

095

096

097

098

099

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

activity_main.xml 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayoutxmlns:androidLinearLayoutxmlns: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:androidLinearLayoutxmlns: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/android-basic-control

原文链接:http://www.apkbus.com/blog-894741-63323.html

0人推荐
随时随地看视频
慕课网APP