zoukankan      html  css  js  c++  java
  • 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设置上动画,这样在弹出的时候,不会显得那么突兀,哈哈  
    如果有什么地方是错误的,请大家批评指正。

  • 相关阅读:
    UNIGUI如何禁止关闭新窗口
    【转】华为编码规范
    awk中 使用shell的环境变量
    【转】SDL与MFC的混合
    MSSQL学习笔记
    转 在.NET环境下为网站增加IP过滤功能
    欢迎加入asp.net交流群
    配置SQL Server2005以允许远程访问
    实用的文件操作类
    VS2005 + VSS2005 实现团队开发、源代码管理、版本
  • 原文地址:https://www.cnblogs.com/zhaohongtian/p/6801587.html
Copyright © 2011-2022 走看看