Android之PopupWindow弹出对话框 Android之PopupWindow弹出对话框
Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:
Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,PopupWindow弹出的位置可以很多变化,按照有无偏移分,可以分为无偏移和偏移两种;按照参照类型不同又可以分为两种:相对某个控件(Anchor锚)的位置和父容器内部的相对位置。具体如下:
函数 | 简介 |
showAsDropDown(Viewanchor) | 相对某个控件的位置(正左下方),无偏移 |
showAsDropDown(Viewanchor, int xoff, int yoff) | 相对某个控件的位置,有偏移 |
showAtLocation(Viewparent, int gravity, int x, int y) | 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等 |
下面是运行程序截图:
程序代码:
布局:main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/layout"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/tv_showText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="@string/hello"
- android:textSize="22px"
- />
- <Button
- android:id="@+id/bt_PopupWindow1"
- android:text="以自己为Anchor,不偏移"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow2"
- android:text="以自己为Anchor,正下方"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow3"
- android:text="以屏幕中心为参照,不偏移(正中间)"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow4"
- android:text="以屏幕下方为参照,下方中间"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
自定义对话框dialog.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/tv_tip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请输入内容:"
- />
- <EditText
- android:id="@+id/et_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- ></EditText>
- <LinearLayout
- android:gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/bt_ok"
- android:text="确定"
- android:layout_width="100px"
- android:layout_height="50px"
- />
- <Button
- android:id="@+id/bt_cancle"
- android:text="取消"
- android:layout_width="100px"
- android:layout_height="50px"
- />
- </LinearLayout>
- </LinearLayout>
代码:
- packagecom.myandroid.test;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.content.SharedPreferences.Editor;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.Gravity;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.ViewGroup.LayoutParams;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.Gallery;
- importandroid.widget.PopupWindow;
- importandroid.widget.TextView;
- publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。
- privateButtonbt_popupWindow1;
- privateButtonbt_popupWindow2;
- privateButtonbt_popupWindow3;
- privateButtonbt_popupWindow4;
- privateTextViewtv_showText;
- privatePopupWindowpopupWindow;
- privateintscreenWidth;
- privateintscreenHeight;
- privateintdialgoWidth;
- privateintdialgoheight;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- /**
- *初始化控件和响应事件
- */
- privatevoidinitView(){
- bt_popupWindow1=(Button)findViewById(R.id.bt_PopupWindow1);
- bt_popupWindow2=(Button)findViewById(R.id.bt_PopupWindow2);
- bt_popupWindow3=(Button)findViewById(R.id.bt_PopupWindow3);
- bt_popupWindow4=(Button)findViewById(R.id.bt_PopupWindow4);
- tv_showText=(TextView)findViewById(R.id.tv_showText);
- bt_popupWindow1.setOnClickListener(newClickEvent());
- bt_popupWindow2.setOnClickListener(newClickEvent());
- bt_popupWindow3.setOnClickListener(newClickEvent());
- bt_popupWindow4.setOnClickListener(newClickEvent());
- }
- /**
- *按钮点击事件处理
- *@authorKobi
- *
- */
- privateclassClickEventimplementsOnClickListener{
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- switch(v.getId()){
- caseR.id.bt_PopupWindow1://以自己为Anchor,不偏移
- getPopupWindow();
- popupWindow.showAsDropDown(v);
- break;
- caseR.id.bt_PopupWindow2://以自己为Anchor,偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
- getPopupWindow();
- popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
- break;
- caseR.id.bt_PopupWindow3://以屏幕中心为参照,不偏移
- getPopupWindow();
- popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
- break;
- caseR.id.bt_PopupWindow4://以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
- getPopupWindow();
- popupWindow.showAtLocation(findViewById(R.id.layout),
- Gravity.BOTTOM,0,0);
- break;
- default:
- break;
- }
- }
- }
- /**
- *创建PopupWindow
- */
- protectedvoidinitPopuptWindow(){
- //TODOAuto-generatedmethodstub
- ViewpopupWindow_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
- R.layout.dialog,null,false);
- popupWindow=newPopupWindow(popupWindow_view,200,150,true);//创建PopupWindow实例
- Buttonbt_ok=(Button)popupWindow_view.findViewById(R.id.bt_ok);//dialog.xml视图里面的控件
- Buttonbt_cancle=(Button)popupWindow_view.findViewById(R.id.bt_cancle);
- finalEditTextet_text=(EditText)popupWindow_view.findViewById(R.id.et_text);
- bt_ok.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- tv_showText.setText(et_text.getText());//在标签里显示内容
- popupWindow.dismiss();//对话框消失
- }
- });
- bt_cancle.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- popupWindow.dismiss();
- }
- });
- //获取屏幕和对话框各自高宽
- screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
- screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
- dialgoWidth=popupWindow.getWidth();
- dialgoheight=popupWindow.getHeight();
- }
- /*
- *获取PopupWindow实例
- */
- privatevoidgetPopupWindow(){
- if(null!=popupWindow){
- popupWindow.dismiss();
- return;
- }else{
- initPopuptWindow();
- }
- }
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- super.onPause();
- Log.e("ActivityState","onPause");
- }
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- Log.e("ActivityState","onResume");
- }
- }
函数 | 简介 |
showAsDropDown(Viewanchor) | 相对某个控件的位置(正左下方),无偏移 |
showAsDropDown(Viewanchor, int xoff, int yoff) | 相对某个控件的位置,有偏移 |
showAtLocation(Viewparent, int gravity, int x, int y) | 父容器容器相对位置,例如正中央Gravity.CENTER,下方Gravity.BOTTOM等 |
下面是运行程序截图:
程序代码:
布局:main.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/layout"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/tv_showText"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:gravity="center"
- android:text="@string/hello"
- android:textSize="22px"
- />
- <Button
- android:id="@+id/bt_PopupWindow1"
- android:text="以自己为Anchor,不偏移"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow2"
- android:text="以自己为Anchor,正下方"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow3"
- android:text="以屏幕中心为参照,不偏移(正中间)"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/bt_PopupWindow4"
- android:text="以屏幕下方为参照,下方中间"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
自定义对话框dialog.xml
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/tv_tip"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="请输入内容:"
- />
- <EditText
- android:id="@+id/et_text"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- ></EditText>
- <LinearLayout
- android:gravity="center_horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/bt_ok"
- android:text="确定"
- android:layout_width="100px"
- android:layout_height="50px"
- />
- <Button
- android:id="@+id/bt_cancle"
- android:text="取消"
- android:layout_width="100px"
- android:layout_height="50px"
- />
- </LinearLayout>
- </LinearLayout>
代码:
- packagecom.myandroid.test;
- importandroid.app.Activity;
- importandroid.content.Context;
- importandroid.content.SharedPreferences.Editor;
- importandroid.os.Bundle;
- importandroid.util.Log;
- importandroid.view.Gravity;
- importandroid.view.LayoutInflater;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.view.ViewGroup.LayoutParams;
- importandroid.widget.Button;
- importandroid.widget.EditText;
- importandroid.widget.Gallery;
- importandroid.widget.PopupWindow;
- importandroid.widget.TextView;
- publicclassPopupWindowTestextendsActivity{//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。
- privateButtonbt_popupWindow1;
- privateButtonbt_popupWindow2;
- privateButtonbt_popupWindow3;
- privateButtonbt_popupWindow4;
- privateTextViewtv_showText;
- privatePopupWindowpopupWindow;
- privateintscreenWidth;
- privateintscreenHeight;
- privateintdialgoWidth;
- privateintdialgoheight;
- /**Calledwhentheactivityisfirstcreated.*/
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- /**
- *初始化控件和响应事件
- */
- privatevoidinitView(){
- bt_popupWindow1=(Button)findViewById(R.id.bt_PopupWindow1);
- bt_popupWindow2=(Button)findViewById(R.id.bt_PopupWindow2);
- bt_popupWindow3=(Button)findViewById(R.id.bt_PopupWindow3);
- bt_popupWindow4=(Button)findViewById(R.id.bt_PopupWindow4);
- tv_showText=(TextView)findViewById(R.id.tv_showText);
- bt_popupWindow1.setOnClickListener(newClickEvent());
- bt_popupWindow2.setOnClickListener(newClickEvent());
- bt_popupWindow3.setOnClickListener(newClickEvent());
- bt_popupWindow4.setOnClickListener(newClickEvent());
- }
- /**
- *按钮点击事件处理
- *@authorKobi
- *
- */
- privateclassClickEventimplementsOnClickListener{
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- switch(v.getId()){
- caseR.id.bt_PopupWindow1://以自己为Anchor,不偏移
- getPopupWindow();
- popupWindow.showAsDropDown(v);
- break;
- caseR.id.bt_PopupWindow2://以自己为Anchor,偏移(screenWidth-dialgoWidth)/2,0)--按钮正下方
- getPopupWindow();
- popupWindow.showAsDropDown(v,(screenWidth-dialgoWidth)/2,0);
- break;
- caseR.id.bt_PopupWindow3://以屏幕中心为参照,不偏移
- getPopupWindow();
- popupWindow.showAtLocation(findViewById(R.id.layout),Gravity.CENTER,0,0);
- break;
- caseR.id.bt_PopupWindow4://以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2,0)--屏幕下方中央
- getPopupWindow();
- popupWindow.showAtLocation(findViewById(R.id.layout),
- Gravity.BOTTOM,0,0);
- break;
- default:
- break;
- }
- }
- }
- /**
- *创建PopupWindow
- */
- protectedvoidinitPopuptWindow(){
- //TODOAuto-generatedmethodstub
- ViewpopupWindow_view=getLayoutInflater().inflate(//获取自定义布局文件dialog.xml的视图
- R.layout.dialog,null,false);
- popupWindow=newPopupWindow(popupWindow_view,200,150,true);//创建PopupWindow实例
- Buttonbt_ok=(Button)popupWindow_view.findViewById(R.id.bt_ok);//dialog.xml视图里面的控件
- Buttonbt_cancle=(Button)popupWindow_view.findViewById(R.id.bt_cancle);
- finalEditTextet_text=(EditText)popupWindow_view.findViewById(R.id.et_text);
- bt_ok.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- tv_showText.setText(et_text.getText());//在标签里显示内容
- popupWindow.dismiss();//对话框消失
- }
- });
- bt_cancle.setOnClickListener(newOnClickListener(){
- @Override
- publicvoidonClick(Viewv){
- //TODOAuto-generatedmethodstub
- popupWindow.dismiss();
- }
- });
- //获取屏幕和对话框各自高宽
- screenWidth=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();
- screenHeight=PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();
- dialgoWidth=popupWindow.getWidth();
- dialgoheight=popupWindow.getHeight();
- }
- /*
- *获取PopupWindow实例
- */
- privatevoidgetPopupWindow(){
- if(null!=popupWindow){
- popupWindow.dismiss();
- return;
- }else{
- initPopuptWindow();
- }
- }
- @Override
- protectedvoidonPause(){
- //TODOAuto-generatedmethodstub
- super.onPause();
- Log.e("ActivityState","onPause");
- }
- @Override
- protectedvoidonResume(){
- //TODOAuto-generatedmethodstub
- super.onResume();
- Log.e("ActivityState","onResume");
- }
- }