Android学习之PopupWindow

     Android的对话框有两种:PopupWindow和AlertDialog。 详细说明如下: AlertDialog是非阻塞式对话框:AlertDialog弹出时,后台还可以做事情; AlertDialog的位置固定,而PopupWindow的位置可以随意; AlertDialog弹出时,背景是黑色的,但是当我们点击背景,AlertDialog会消失,证明程序不仅响应AlertDialog的操作,还响应其他操作,其他程序没有被阻塞,这说明了AlertDialog是非阻塞式对话框; PopupWindow是阻塞式对话框:PopupWindow弹出时,程序会等待,在PopupWindow退出前,程序一直等待,只有当我们调用了dismiss方法的后,PopupWindow退出,程序才会向下执行。 PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件; PopupWindow弹出时,背景没有什么变化,但是当我们点击背景的时候,程序没有响应,只允许我们操作PopupWindow,其他操作被阻塞。

一.使用popupWindow只需2步即可:

1.调用popupWindow的构造器创建PopupWindow对象;

2.调用popupWindow的方法将popupWindow显示出来,方法有三种:

    (1)popupWindow.showAtLocation(btn, Gravity.CENTER, 0, 0);

    (2)popupWindow.showAsDropDown(btn);//相对某个控件的位置(正左下方),无偏移

     (3)popupWindow.showAsDropDown(btn, 10, 10);//相对某个控件的位置,有偏移

二.其它常用设置:

    (1)popupWindow.setFocusable(true);//默认是false,当设置为true时,系统会捕获到焦点给popupWindow; 设置此参数获得焦点,否则无法点击;  

    (2)popupWindow.setOutsideTouchable(true); //点击PopupWindow区域外部,PopupWindow消失  

    (3)popupWindow.setAnimationStyle(R.style.PopupAnimation); //可以设置动画  

    (4)new PopupWindow(view,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);//在构造函数中可以设置popupwindow的大小;

三. 简单实例:

1.效果图:

2.XML:

activity_main页面:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:gravity="center_horizontal"
 6     android:orientation="vertical" >
 7 
 8     <Button
 9         android:id="@+id/btn"
10         android:layout_width="wrap_content"
11         android:layout_height="wrap_content"
12         android:text="弹出泡泡窗口" />
13 
14 </LinearLayout>

popup页面:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:gravity="center_horizontal"
 6     android:orientation="vertical" >
 7 
 8     <ImageView
 9         android:layout_width="240dp"
10         android:layout_height="wrap_content"
11         android:src="@drawable/img" />
12 
13     <Button
14         android:id="@+id/close"
15         android:layout_width="wrap_content"
16         android:layout_height="wrap_content"
17         android:text="关闭" />
18 
19 </LinearLayout>

3.java代码:

 1 package com.example.popupwindow;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 import android.view.Gravity;
 6 import android.view.View;
 7 import android.view.View.OnClickListener;
 8 import android.widget.Button;
 9 import android.widget.PopupWindow;
10 
11 public class MainActivity extends Activity {
12 
13     private Button btn;
14     private Button btnClose;
15     private View inflaterView;
16 
17     @Override
18     public void onCreate(Bundle savedInstanceState) {
19         super.onCreate(savedInstanceState);
20         setContentView(R.layout.activity_main);
21 
22         initView();
23         
24     }
25 
26     private void initView() {
27         // 装载R.layout.popup对应的界面布局
28         inflaterView = this.getLayoutInflater().inflate(R.layout.popup, null);
29         btnClose = (Button) inflaterView.findViewById(R.id.close);
30         btn = (Button) this.findViewById(R.id.btn);
31 
32         final PopupWindow popupWindow = new PopupWindow(inflaterView, 300, 400);
33         btn.setOnClickListener(new OnClickListener() {
34             @Override
35             public void onClick(View arg0) {
36                 popupWindow.showAtLocation(btn, Gravity.CENTER, 20, 20);
37 
38             }
39         });
40 
41         btnClose.setOnClickListener(new OnClickListener() {
42             @Override
43             public void onClick(View arg0) {
44                 popupWindow.dismiss(); // 关闭窗口
45             }
46         });
47     }
48 }
原文地址:https://www.cnblogs.com/summers/p/4086927.html