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

时间: 2025-01-05 01:44:48

Android—PopupWindow的简单使用的相关文章

Android PopupWindow 仿微信弹出效果

项目中,我需要PopupWindow的时候特别多,这个东西也特别的好使,所以我今天给大家写一款PopupWindow 仿微信弹出效果,这样大家直接拿到项目里就可以用了!首先让我们先看效果: 那么我首先先看下布局代码非常简单:如下 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/pop_layout" android:layout_

Android PopupWindow使用方法小结

前几天要用到PopupWindow,一时竟想不起来怎么用,赶紧上网查了查,自己写了个demo,并在此记录一下PopupWindow的用法. 使用场景 PopupWindow,顾名思义,就是弹窗,在很多场景下都可以见到它.例如ActionBar/Toolbar的选项弹窗,一组选项的容器,或者列表等集合的窗口等等. 基本用法 使用PopupWindow很简单,可以总结为三个步骤: 创建PopupWindow对象实例: 设置背景.注册事件监听器和添加动画: 显示PopupWindow. 其中,第二步是

Android ExpandableListView的简单应用

Expandablelistview1Activity.java package com.wangzhu.demoexpandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.widg

【原创】android——SQLite实现简单的注册登陆(已经美化)

1,Main_activity的xmL配置 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_pa

Android popupwindow以及windowManager总结——实现悬浮效果

Android有三类窗口 应用程序窗口 (Application Window): 包括所有应用程序自己创建的窗口,以及在应用起来之前系统负责显示的窗口. 子窗口(Sub Window):比如应用自定义的对话框,或者输入法窗口,子窗口必须依附于某个应用窗口(设置相同的token). 系 统窗口(System Window): 系统设计的,不依附于任何应用的窗口,比如说,状态栏(Status Bar), 导航栏(Navigation Bar), 壁纸(Wallpaper), 来电显示窗口(Phon

【Android】android PopupWindow实现从底部弹出或滑出选择菜单或窗口

转载自:android PopupWindow实现从底部弹出或滑出选择菜单或窗口 Android PopupWindow的使用和分析 Popupwindow的使用 PopupWindow用法

Android HttpGet() 请求简单入门实例

HttpClient httpclient = new DefaultHttpClient(); String url = "http://example.com"; List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add( new BasicNameValuePair( "param", "value" ) ); URI uri =

Android PopupWindow显示位置和显示大小

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:bac

【android】Socket简单用法

原文地址:http://www.cnblogs.com/harrisonpc/archive/2011/03/31/2001565.html Socket通常也称做”套接字“,用于描述IP地址和端口,废话不多说,它就是网络通信过程中端点的抽象表示.值得一提的是,Java在包java.net中提供了两个类Socket和ServerSocket,分别用来表示双向连接的客户端和服务端.这是两个封装得非常好的类,使用起来很方便! 下面将首先创建一个SocketServer的类作为服务端如下,该服务端实现