Use of Popupwindow

PopupWindow is under the Android.widget package. PopupWindow is often used in projects as a menu option. The PopupWindow class is used to implement a pop-up box, which can use any layout of View as its content. This pop-up box is suspended above the current activity. .

 

Effect picture:

 

 

 

 

MainActivity.java

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();
          // Set the View of SelectPicPopupWindow
          mPopWindow.setContentView(contentView);
          // Set the width of the SelectPicPopupWindow popup form
          mPopWindow.setWidth(LayoutParams.WRAP_CONTENT);
          // Set the height of the SelectPicPopupWindow popup form
          mPopWindow.setHeight(LayoutParams.WRAP_CONTENT);
          // Set the SelectPicPopupWindow popup form to be clickable
         mPopWindow.setFocusable(true);
         mPopWindow.setOutsideTouchable(true);
         // refresh state
         mPopWindow.update();
         // Instantiate a ColorDrawable with a semi-transparent color
         ColorDrawable dw = new ColorDrawable(0000000000);
         // Click the back key and other places to make it disappear, set this to trigger OnDismisslistener, set other control changes, etc.
         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();
     }
  }

 

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="message"
                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="Favorites"
                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="Home"
                android:textColor="#FFFFFF"/>
        </RelativeLayout>
 
        <View
            android:layout_width="wrap_content"
            android:layout_height="1dp"/>
    </LinearLayout>
 
</LinearLayout>

 

 

Source code click to download

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326256227&siteId=291194637