MrDuoduo 2014-06-05
[b]Android中的PopupWindow详解[/b]
Android的对话框有两种:PopupWindow和AlertDialog。它们的不同点在于:
AlertDialog的位置固定,而PopupWindow的位置可以随意
AlertDialog是非阻塞线程的,而PopupWindow是阻塞线程的
PopupWindow的位置按照有无偏移分,可以分为偏移和无偏移两种;按照参照物的不同,可以分为相对于某个控件(Anchor锚)和相对于父控件。具体如下
showAsDropDown(Viewanchor):相对某个控件的位置(正左下方),无偏移
showAsDropDown(Viewanchor,intxoff,intyoff):相对某个控件的位置,有偏移
showAtLocation(Viewparent,intgravity,intx,inty):相对于父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以设置偏移或无偏移
下面通过一个Demo讲解(解释看注释):
main.xml
[html]<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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
[html]
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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">
<RadioButtonandroid:text="在线"/>
<RadioButtonandroid:text="离线"/>
<RadioButtonandroid:text="隐身"/>
</RadioGroup>
</LinearLayout>
PopupWindowDemoActivity.java
[java]
packagecom.tianjf;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.Gravity;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.view.View.OnClickListener;
importandroid.widget.Button;
importandroid.widget.PopupWindow;
importandroid.widget.RadioGroup;
importandroid.widget.RadioGroup.OnCheckedChangeListener;
publicclassPopupWindowDemoActivityextendsActivityimplementsOnClickListener,
OnCheckedChangeListener{
privateButtonmbutton01;
privateButtonmbutton02;
privateButtonmbutton03;
privateButtonmbutton04;
privatePopupWindowmPopupWindow;
//屏幕的width
privateintmScreenWidth;
//屏幕的height
privateintmScreenHeight;
//PopupWindow的width
privateintmPopupWindowWidth;
//PopupWindow的height
privateintmPopupWindowHeight;
@Override
publicvoidonCreate(BundlesavedInstanceState){
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
publicvoidonClick(Viewv){
switch(v.getId()){
//相对某个控件的位置(正左下方),无偏移
caseR.id.button01:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v);
break;
//相对某个控件的位置(正左下方),有偏移
caseR.id.button02:
getPopupWindowInstance();
mPopupWindow.showAsDropDown(v,50,50);//X、Y方向各偏移50
break;
//相对于父控件的位置,无偏移
caseR.id.button03:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v,Gravity.CENTER,0,0);
break;
//相对于父控件的位置,有偏移
caseR.id.button04:
getPopupWindowInstance();
mPopupWindow.showAtLocation(v,Gravity.BOTTOM,0,50);
break;
default:
break;
}
}
@Override
publicvoidonCheckedChanged(RadioGroupgroup,intcheckedId){
mPopupWindow.dismiss();
}
/*
*获取PopupWindow实例
*/
privatevoidgetPopupWindowInstance(){
if(null!=mPopupWindow){
mPopupWindow.dismiss();
return;
}else{
initPopuptWindow();
}
}
/*
*创建PopupWindow
*/
privatevoidinitPopuptWindow(){
LayoutInflaterlayoutInflater=LayoutInflater.from(this);
ViewpopupWindow=layoutInflater.inflate(R.layout.popup_window,null);
RadioGroupradioGroup=(RadioGroup)popupWindow.findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(this);
//创建一个PopupWindow
//参数1:contentView指定PopupWindow的内容
//参数2:width指定PopupWindow的width
//参数3:height指定PopupWindow的height
mPopupWindow=newPopupWindow(popupWindow,100,130);
//获取屏幕和PopupWindow的width和height
mScreenWidth=getWindowManager().getDefaultDisplay().getWidth();
mScreenWidth=getWindowManager().getDefaultDisplay().getHeight();
mPopupWindowWidth=mPopupWindow.getWidth();
mPopupWindowHeight=mPopupWindow.getHeight();
}
}