度过了一个冬眠,在春末夏初之际,补充自己的营养,努力学习,努力工作!

Android中的PopupWindow详解

上一篇 / 下一篇  2012-11-21 17:17:32 / 个人分类:android

51Testing软件测试网Z(C A!y1~T.X

Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
j wKzQ$w+S0AlertDialog的位置固定,而PopupWindow的位置可以随意51Testing软件测试网 W(e\_4?8Q
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
+?"n!f%Di ~0PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
n0L T.Z r dv&q0showAsDropDown(View anchor):相对某个控件的位置(正左下方),无偏移
[uWd!o^^"A0showAsDropDown(View anchor, int xoff, int yoff):相对某个控件的位置,有偏移51Testing软件测试网K9gval|w
showAtLocation(View parent, int gravity, int x, int y):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移51Testing软件测试网&c j^;Swe
下面通过一个Demo讲解(解释看注释):51Testing软件测试网q&f8t A-B;}&_
main.xml51Testing软件测试网4I&i\U `&Kg
[html] <?xml version="1.0" encoding="utf-8"?> 
9i"_3r-g@*NOk.n0<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
M~jk5z4o0    android:layout_width="fill_parent" 51Testing软件测试网l:_ DXa5HJ
    android:layout_height="fill_parent" 
B#jJcG cj0    android:orientation="vertical" > 
b4h%g+Q@0 
a qo)mh`0    <TextView 51Testing软件测试网Xy g{8P
        android:layout_width="fill_parent" 
QhL f7yi _O Gf0        android:layout_height="wrap_content" 
2wcD0d }%Z1[7F&[0        android:text="@string/hello" /> 51Testing软件测试网%O] ^#rou5o
 
I%UoBx8zS&s0    <Button 51Testing软件测试网YkehU U7^ Y"C
        android:id="@+id/button01" 
l)F Zz&\2c0        android:layout_width="fill_parent" 51Testing软件测试网U3q6F2Px
        android:layout_height="wrap_content" 51Testing软件测试网}qM3k!{1vT gW
        android:text="以自己为Anchor,不偏移" /> 
/G.qrK&AE8I`0 
Z*n$Ifb#kQ7K0    <Button 51Testing软件测试网I]8~1tc$w(z6BL
        android:id="@+id/button02" 51Testing软件测试网g3RX3wD%b
        android:layout_width="fill_parent" 
eEZZ U1SR r0        android:layout_height="wrap_content" 51Testing软件测试网"h \9Gp$XuTd,e
        android:text="以自己为Anchor,有偏移" /> 
]'^V+t8XN0 51Testing软件测试网?EB l!?'A
    <Button 51Testing软件测试网M;^4|qty FBZ
        android:id="@+id/button03" 
p+C#| E(A%Pkl0        android:layout_width="fill_parent" 
*dJ7} ]+]1W]TeF0m,N0        android:layout_height="wrap_content" 
.R}@UZ-O:}!X0        android:text="以屏幕中心为参照,不偏移(正中间)" /> 51Testing软件测试网t#T/Iv;dZ:o
 51Testing软件测试网2Rp%s|)[%s9`I ^
    <Button 
.d[M'ko-amq.z*_c G-O0        android:id="@+id/button04" 51Testing软件测试网"B_#jaW&f"v:DH
        android:layout_width="fill_parent" 51Testing软件测试网``[ [~ j
        android:layout_height="wrap_content" 
?&K!]P:S%v#X5`0        android:text="以屏幕下方为参照,下方中间" /> 
L:w"s-aV] r} ]0 
AfQi"``v0</LinearLayout> 51Testing软件测试网 c8F b-JIt3S$YZ2M

51Testing软件测试网 |vWV7M4Gg |a

popup_window.xml51Testing软件测试网!|~m [f Q]!h*KI
[html]51Testing软件测试网W%D X2J-@6K4E
<?xml version="1.0" encoding="utf-8"?> 51Testing软件测试网7n*s7z%t4y E9V/a
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android
5g$z7{9Hi\0    android:layout_width="wrap_content" 51Testing软件测试网6E? mh2VJqV
    android:layout_height="wrap_content" 
b7IX JFv R0    android:background="#00FF00" 51Testing软件测试网2Af ~/[aW{1O.l*~"M
    android:orientation="vertical" > 
$cc.Sa9?9cXg0 51Testing软件测试网9C?3Mp/gd
    <TextView 51Testing软件测试网/Z GAGe
        android:layout_width="wrap_content" 51Testing软件测试网 C+s$G Z w*b m7u
        android:layout_height="wrap_content" 51Testing软件测试网x1h7wB"g
        android:text="选择状态:" 51Testing软件测试网Fil,p:Pq,h
        android:textColor="@android:color/white" 51Testing软件测试网'O8S LC @0k s(r&X
        android:textSize="20px" /> 51Testing软件测试网TQ|HhFX
 
i]1UPM$K.xpc0    <RadioGroup 
,N0M;h/K,Bt0        android:id="@+id/radioGroup" 51Testing软件测试网 ylu Q!Zv\
        android:layout_width="wrap_content" 
1vd_B$j s$X m0        android:layout_height="wrap_content" 
)M e}1~7vk0        android:orientation="vertical" > 51Testing软件测试网}]6{` PRe }N~ B%x
 
rd7G }w#PA0        <RadioButton android:text="在线" /> 
#{&@l7JU;\#rr E0 
qC$Q.b P~1V0        <RadioButton android:text="离线" /> 51Testing软件测试网 lcJn QC/^E
 51Testing软件测试网GP)z%x p*h,UX5O
        <RadioButton android:text="隐身" /> 51Testing软件测试网@ e"R DB
    </RadioGroup> 51Testing软件测试网p5_2N+MU.GyR
 
7a@5]M `y:@,D$ek0</LinearLayout> 51Testing软件测试网%Eh H/G1C

51Testing软件测试网%D;f*C,AV a5Bk'z2m

PopupWindowDemoActivity.java
-|%pV1}h@0[java]
5j){3]CAk o(ku0package com.tianjf; 51Testing软件测试网/Ekz4z'f2d9Y
 51Testing软件测试网M\;\g Y^D
import android.app.Activity; 
.N`&TVAR0import android.os.Bundle; 
+g$~wo*iCY0import android.view.Gravity; 
K J6N\8NF:Y0import android.view.LayoutInflater; 51Testing软件测试网9h_\` u/z
import android.view.View; 
#M9RD?.AG ZTd3F0import android.view.View.OnClickListener; 51Testing软件测试网(_%~:C0q6NWLDN
import android.widget.Button; 
[Yh z#K` F"}]0import android.widget.PopupWindow; 
`q#R1O/iLqX SP+`a2I+T0import android.widget.RadioGroup; 51Testing软件测试网 pN!e+bKbo'`0s$S
import android.widget.RadioGroup.OnCheckedChangeListener; 51Testing软件测试网7V ])b{9{H9t-b
 
gn1Ef*|K0a0public class PopupWindowDemoActivity extends Activity implements OnClickListener, 51Testing软件测试网^R7]]L1}Q
        OnCheckedChangeListener { 51Testing软件测试网GD1OuFPr
 51Testing软件测试网 Sn`g(^'F
    private Button mbutton01; 
1uUm!h _#M%Q#E0    private Button mbutton02; 
sI}RD)k,LK$X` N0RK0    private Button mbutton03; 51Testing软件测试网+z"wk-L)f4rA~7W
    private Button mbutton04; 51Testing软件测试网%Cq5K&D"mEC
    private PopupWindow mPopupWindow; 
-VKsBR0    // 屏幕的width 
hyM:l,mLP0    private int mScreenWidth; 
P)Vw#S_0    // 屏幕的height 51Testing软件测试网&iYqgH#z6gG
    private int mScreenHeight; 51Testing软件测试网.VYET7Zw j+Q*rm
    // PopupWindow的width 
5Q@I }%x0    private int mPopupWindowWidth; 
!Ts x;e\}0    // PopupWindow的height 
3e p5D2N&W x0    private int mPopupWindowHeight; 51Testing软件测试网 ]:g8[1upt| j;D E?I
 
/hfZS8m@MY0    @Override 51Testing软件测试网D9G)\F%[[V
    public void onCreate(Bundle savedInstanceState) { 51Testing软件测试网6`@!e*[Ie!b7N
        super.onCreate(savedInstanceState); 51Testing软件测试网P2x!Y_P'^
        setContentView(R.layout.main); 
oSQr @N0 51Testing软件测试网M!N G?8N&K!h N$KA
        mbutton01 = (Button) findViewById(R.id.button01); 
L"r6x+|T4L5es~%P0        mbutton02 = (Button) findViewById(R.id.button02); 51Testing软件测试网6_#tA(I!hS
        mbutton03 = (Button) findViewById(R.id.button03); 51Testing软件测试网3]EtJ#n%g%bu#h O
        mbutton04 = (Button) findViewById(R.id.button04); 
Id!@"[Gca @5P'W0 51Testing软件测试网 e;p D Q Z-A
        mbutton01.setOnClickListener(this); 51Testing软件测试网Bh L q5G8e9J#ksI
        mbutton02.setOnClickListener(this); 
$C+| ~C#V/fvu5dhs0        mbutton03.setOnClickListener(this); 
5f'v$yk2@5I0        mbutton04.setOnClickListener(this); 51Testing软件测试网:WT)G5G'dx
    } 
5q%m:_\ bwF.GR0 
B.?l,G/y.} }&]-H Ti|H0    @Override 51Testing软件测试网.`-EdgO.zh'`6Ue0v e
    public void onClick(View v) { 
2k E]g ^ ppE0        switch (v.getId()) { 
2e]K ns zV8d/U0        // 相对某个控件的位置(正左下方),无偏移 51Testing软件测试网3G1E#M#FbZA9B2X
        case R.id.button01: 
P E}2f0w,] `;D0            getPopupWindowInstance(); 
L.lk&g&\zoob0            mPopupWindow.showAsDropDown(v); 
+V+e V0jo H0            break; 
~9O#}/ErvV pv$g0 
JX2TxQ7L1]B\0        // 相对某个控件的位置(正左下方),有偏移 51Testing软件测试网L0N)l]G7xY
        case R.id.button02: 
ji3} pZ`u#^0            getPopupWindowInstance(); 
p)AF3u(TzK H0            mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50 
1{3~aOyC;f1?0            break; 
n R e v.Ae/Z0 51Testing软件测试网E1^3^Md9iI_)Z
        // 相对于父控件的位置,无偏移 51Testing软件测试网F"}+J/C7kq M
        case R.id.button03: 51Testing软件测试网zE2k'L(N(g.R
            getPopupWindowInstance(); 51Testing软件测试网+o$Yn5w])uNZ/O@6|#@
            mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0); 51Testing软件测试网 sv `:Pd"c
            break; 51Testing软件测试网4Q7O`B"k;a/K$a
 51Testing软件测试网:j4m3LkT^9q [
        // 相对于父控件的位置,有偏移 51Testing软件测试网 |a'A k ?C _
        case R.id.button04: 51Testing软件测试网Ls']l CB[#Zc$E
            getPopupWindowInstance(); 
5{j!k(l`)[1neN{N0            mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50); 
*Wq Q'KxZ}|L0            break; 
N9] k%Y}%W0 51Testing软件测试网 ha8^ [r9J
        default: 
v9z+F |'Z;l5C'_0            break; 51Testing软件测试网P0c#mx-M:N+K
        } 51Testing软件测试网 Jk+LN)?
    } 
%l%IR)};{)f)l0 
?4{3w"N}s gq0    @Override 51Testing软件测试网twu^&~ \r(s
    public void onCheckedChanged(RadioGroup group, int checkedId) { 
2\ UB,B}@5e1R0        mPopupWindow.dismiss(); 
L[nw h"?D0    } 51Testing软件测试网c] s `)e5K)F8Cr5r
 
#rU0iFD0    /*
K3e F,b$M0     * 获取PopupWindow实例51Testing软件测试网 @"W b^xH*P
     */ 
T H?M6G"f0    private void getPopupWindowInstance() { 
*Yb@ c0f ^&X*T0        if (null != mPopupWindow) { 
:[${bbASoQ ]ZS,]0            mPopupWindow.dismiss(); 51Testing软件测试网,_"OHJI5A`O d
            return; 51Testing软件测试网UN{*l1mi-`&m
        } else { 
a_-k!rk6eH!C0            initPopuptWindow(); 51Testing软件测试网Vh XI!^3d
        } 
u{%~*L&g2Y0    } 
6vOk&X[%h6n E0 
*C.C.[ fx0    /*
]S&I.p$w8HjK H0     * 创建PopupWindow51Testing软件测试网N3_6x2x~\
     */ 
(K1^Fm&f$?0    private void initPopuptWindow() { 
p/\,z+X J&Y-I0        LayoutInflater layoutInflater = LayoutInflater.from(this); 
;D5O9p6~I;L#Cp8S_0        View popupWindow = layoutInflater.inflate(R.layout.popup_window, null); 51Testing软件测试网g#[n*ma;d A:}h#[
        RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup); 
8Q|ew7LW x0        radioGroup.setOnCheckedChangeListener(this); 51Testing软件测试网$du.m,g#L%KcMh1L
 
c/O'HDK!c6_9p0        // 创建一个PopupWindow 51Testing软件测试网D3uN5} xVe9k4F
        // 参数1:contentView 指定PopupWindow的内容 
$E*kD#y7vA0        // 参数2:width 指定PopupWindow的width 
/uUQP%w0        // 参数3:height 指定PopupWindow的height 
yvc,_!m0        mPopupWindow = new PopupWindow(popupWindow, 100, 130); 51Testing软件测试网1Se xR!\ E]i
 51Testing软件测试网6Za W-Y2M
        // 获取屏幕和PopupWindow的width和height 
R,V7`)J0U$o!K0        mScreenWidth = getWindowManager().getDefaultDisplay().getWidth(); 51Testing软件测试网un&Z8]'J6o&RNW
        mScreenWidth = getWindowManager().getDefaultDisplay().getHeight(); 
7U&k%q fD b)I]~0        mPopupWindowWidth = mPopupWindow.getWidth(); 
xCZ8[q+aG8jt0        mPopupWindowHeight = mPopupWindow.getHeight(); 51Testing软件测试网:LW OT^E~
    } 
2g%k[q_h;B)T0

i4Kv |'cC0

d)K3gH:AL(wY0 

]Ok;B,d;\U)f^051Testing软件测试网z'fqA1c0{0fw+J:g}

TAG:

 

评分:0

我来说两句

Open Toolbar