Android—PopupWindow的简单使用

PopupWindow 是一个可以显示在当前 Activity 之上的浮动容器,这个Demo要实现的功能是,点击布局中的两个按钮,进而控制PopupWindow的显示与消失,代码中有详细的注释首先看一下效果展示:

在上代码之前,先总结一下PopupWindow的用法:

1:实例化PopupWindow的对象,三个参数分别对应:填充的布局文件、在当前Activity上所占的宽、高
PopupWindow popupWindow= new PopupWindow(contentView, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
2:完成第一步所需要的布局文件,并实例出来
View contentView = mLayoutInflater.inflate(R.layout.pop, null)
3:设置PopupWindow 所必备的两个属性
     //popupWindow的背景1)popupWindow.setBackgroundDrawable(......);  
     //popupWindow要显示的位置2)popupWindow.showAtLocation(View parent, int gravity, int x, int y)
接下来,上代码!
popupWindow所要添加的布局文件popu_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#87cdff"
    android:columnCount="4"
    android:orientation="horizontal"
    android:rowCount="4">

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_gongshang" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_guangda" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_jianhang" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_jiaotong" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_minsheng" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_nongye" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_gongshang" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_pingan" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_zhaoshang" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_youzheng" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_xingye" />

    <ImageView
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:layout_margin="12dp"
        android:background="@drawable/icon_bank_pufa" />
</GridLayout>
相当简单的布局,做出来就是这么一个玩意:

MainActivity:

 1 package com.example.wgh.popupwindow;
 2 import android.graphics.drawable.ColorDrawable;
 3 import android.support.v7.app.AppCompatActivity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.GridLayout;
10 import android.widget.PopupWindow;
11 
12 public class MainActivity extends AppCompatActivity {
13 
14     private View mPopView = null;
15     private Button showPopupWindow = null;
16     private Button dismissPopupWindow = null;
17     @Override
18     protected void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         initView();
23         showPopupWindow.setOnClickListener(new View.OnClickListener() {
24             @Override
25             public void onClick(View view) {
26                 showPopupWindow();
27             }
28         });
29     }
30 
31     private void initView() {
32         showPopupWindow = (Button) findViewById(R.id.showPopupWindow);
33         dismissPopupWindow = (Button) findViewById(R.id.dismissPopupWindow);
34         /**
35          * 实例popupWindow要添加的布局
36          */
37         mPopView = LayoutInflater.from(this).inflate(R.layout.popu_layout, null);
38     }
39 
40     private void showPopupWindow() {
41         /**
42          * 实例popupWindow对象
43          */
44         PopupWindow popupWindow = new PopupWindow(mPopView, GridLayout.LayoutParams.MATCH_PARENT, GridLayout.LayoutParams.WRAP_CONTENT);
45         //设置popupWindow中的item可以被点击,这句话是必须要添加的
46         popupWindow.setFocusable(true);
47         //设置PopupWindow的背景
48         //如果不设置背景,会导致无论是点击外部区域还是Back键都无法dismiss掉popupWindow
49         ColorDrawable dw = new ColorDrawable(0xb0000000);
50         popupWindow.setBackgroundDrawable(dw);
51         //设置popupWindow显示的位置
52         popupWindow.showAtLocation(showPopupWindow, Gravity.BOTTOM,0,200);
53     }
54 }
最后说一下关于popupWindow显示位置的属性设置

1 // 相对某个控件的位置(正左下方),无偏移
2 popupWindow.showAsDropDown(View anchor) 
3 // 相对某个控件的位置,有偏移,xoff 为 X 轴的偏移量,yoff 为 Y 轴的偏移量                          
4 popupWindow.showAsDropDown(View anchor, int xoff, int yoff)
5 // 在父容器的什么位置,gravity 为相对位置,
6 //如:正中央 Gravity.CENTER、下方 Gravity.BOTTOM、Gravity.RIGHT|Gravity.BOTTOM 右下方等,后面两个参数为 x/y 轴的偏移量。
7 popupWindow.showAtLocation(View parent, int gravity, int x, int y) 

有兴趣的童鞋可以为popupWindow设置上动画,这样在弹出的时候,不会显得那么突兀,哈哈 
如果有什么地方是错误的,请大家批评指正。

原文地址:https://www.cnblogs.com/upwgh/p/5910261.html