Android中的悬浮效果PopupWindow详解



http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html

http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0302/943.html






Android中的悬浮效果PopupWindow详解

泡在网上的日子 发表于2013-09-26 10:31,   967  次阅读  popupwindow

摘要 Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于: AlertDialog的位置固定,而PopupWindow的位置可以随意 AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的 PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:

  • AlertDialog的位置固定,而PopupWindow的位置可以随意

  • AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的

PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下

  • showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移

  • showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移

  • showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移

下面通过一个Demo讲解(解释看注释):

main.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "fill_parent"
     android:layout_height= "fill_parent"
     android:orientation= "vertical" >
    
     <TextView
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "@string/hello" />
    
     <Button
         android:id= "@+id/button01"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "以自己为Anchor,不偏移" />
    
     <Button
         android:id= "@+id/button02"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "以自己为Anchor,有偏移" />
    
     <Button
         android:id= "@+id/button03"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "以屏幕中心为参照,不偏移(正中间)" />
    
     <Button
         android:id= "@+id/button04"
         android:layout_width= "fill_parent"
         android:layout_height= "wrap_content"
         android:text= "以屏幕下方为参照,下方中间" />
    
</LinearLayout>

popup_window.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
<?xml version= "1.0" encoding= "utf-8" ?>
<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android"
     android:layout_width= "wrap_content"
     android:layout_height= "wrap_content"
     android:background= "#00FF00"
     android:orientation= "vertical" >
    
     <TextView
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:text= "选择状态:"
         android:textColor= "@android:color/white"
         android:textSize= "20px" />
    
     <RadioGroup
         android:id= "@+id/radioGroup"
         android:layout_width= "wrap_content"
         android:layout_height= "wrap_content"
         android:orientation= "vertical" >
    
         <RadioButton android:text= "在线" />
    
         <RadioButton android:text= "离线" />
    
         <RadioButton android:text= "隐身" />
     </RadioGroup>
    
</LinearLayout>

PopupWindowDemoActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package com.tianjf;
    
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
    
public class PopupWindowDemoActivity extends Activity implements OnClickListener,
         OnCheckedChangeListener {
    
     private Button mbutton01;
     private Button mbutton02;
     private Button mbutton03;
     private Button mbutton04;
     private PopupWindow mPopupWindow;
     // 屏幕的width
     private int mScreenWidth;
     // 屏幕的height
     private int mScreenHeight;
     // PopupWindow的width
     private int mPopupWindowWidth;
     // PopupWindow的height
     private int mPopupWindowHeight;
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
    
         mbutton01 = (Button) findViewById(R.id.button01);
         mbutton02 = (Button) findViewById(R.id.button02);
         mbutton03 = (Button) findViewById(R.id.button03);
         mbutton04 = (Button) findViewById(R.id.button04);
    
         mbutton01.setOnClickListener( this );
         mbutton02.setOnClickListener( this );
         mbutton03.setOnClickListener( this );
         mbutton04.setOnClickListener( this );
     }
    
     @Override
     public void onClick(View v) {
         switch (v.getId()) {
         // 相对某个控件的位置(正左下方),无偏移
         case R.id.button01:
             getPopupWindowInstance();
             mPopupWindow.showAsDropDown(v);
             break ;
    
         // 相对某个控件的位置(正左下方),有偏移
         case R.id.button02:
             getPopupWindowInstance();
             mPopupWindow.showAsDropDown(v, 50, 50); // X、Y方向各偏移50
             break ;
    
         // 相对于父控件的位置,无偏移
         case R.id.button03:
             getPopupWindowInstance();
             mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);
             break ;
    
         // 相对于父控件的位置,有偏移
         case R.id.button04:
             getPopupWindowInstance();
             mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);
             break ;
    
         default :
             break ;
         }
     }
    
     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {
         mPopupWindow.dismiss();
     }
    
     /*
      * 获取PopupWindow实例
      */
     private void getPopupWindowInstance() {
         if ( null != mPopupWindow) {
             mPopupWindow.dismiss();
             return ;
         } else {
             initPopuptWindow();
         }
     }
    
     /*
      * 创建PopupWindow
      */
     private void initPopuptWindow() {
         LayoutInflater layoutInflater = LayoutInflater.from( this );
         View popupWindow = layoutInflater.inflate(R.layout.popup_window, null );
         RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);
         radioGroup.setOnCheckedChangeListener( this );
    
         // 创建一个PopupWindow
         // 参数1:contentView 指定PopupWindow的内容
         // 参数2:width 指定PopupWindow的width
         // 参数3:height 指定PopupWindow的height
         mPopupWindow = new PopupWindow(popupWindow, 100, 130);
    
         // 获取屏幕和PopupWindow的width和height
         mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();
         mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();
         mPopupWindowWidth = mPopupWindow.getWidth();
         mPopupWindowHeight = mPopupWindow.getHeight();
     }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值