Android之PopupWindow弹出对话框

Android的对话框常用的有两种:PopupWindow和AlertDialog。PopupWindow顾名思义为弹出菜单,不同于AlertDialog对话框,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等

下面是运行程序截图:

    

     

              

程序代码:

布局:main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
  3.     Android:id="@+id/layout"  
  4.     Android:orientation="vertical"  
  5.     Android:layout_width="fill_parent"  
  6.     Android:layout_height="fill_parent"  
  7.     >  
  8. <TextView    
  9.     Android:id="@+id/tv_showText"  
  10.     Android:layout_width="fill_parent"    
  11.     Android:layout_height="wrap_content"    
  12.     Android:gravity="center"  
  13.     Android:text="@string/hello"  
  14.     Android:textSize="22px"  
  15.     />  
  16.        
  17. <Button  
  18.     Android:id="@+id/bt_PopupWindow1"  
  19.     Android:text="以自己为Anchor,不偏移"  
  20.     Android:layout_width="fill_parent"    
  21.     Android:layout_height="wrap_content"  
  22.     />     
  23.   
  24. <Button  
  25.     Android:id="@+id/bt_PopupWindow2"  
  26.     Android:text="以自己为Anchor,正下方"  
  27.     Android:layout_width="fill_parent"    
  28.     Android:layout_height="wrap_content"  
  29.     />     
  30.        
  31. <Button  
  32.     Android:id="@+id/bt_PopupWindow3"  
  33.     Android:text="以屏幕中心为参照,不偏移(正中间)"  
  34.     Android:layout_width="fill_parent"    
  35.     Android:layout_height="wrap_content"  
  36.     />     
  37.   
  38. <Button  
  39.     Android:id="@+id/bt_PopupWindow4"  
  40.     Android:text="以屏幕下方为参照,下方中间"  
  41.     Android:layout_width="fill_parent"    
  42.     Android:layout_height="wrap_content"  
  43.     />     
  44.        
  45.      
  46. </LinearLayout>  


    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"  
    3.     Android:orientation="vertical"  
    4.     Android:layout_width="fill_parent"  
    5.     Android:layout_height="fill_parent"  
    6.     >  
    7. <TextView    
    8.     Android:id="@+id/tv_tip"  
    9.     Android:layout_width="fill_parent"    
    10.     Android:layout_height="wrap_content"    
    11.     Android:text="请输入内容:"  
    12.     />  
    13.      
    14.  <EditText  
    15.     Android:id="@+id/et_text"  
    16.     Android:layout_width="fill_parent"    
    17.     Android:layout_height="wrap_content"    
    18.  ></EditText>    
    19.     
    20. <LinearLayout  
    21.     Android:gravity="center_horizontal"    
    22.     Android:layout_width="fill_parent"  
    23.     Android:layout_height="fill_parent"  
    24. >     
    25. <Button  
    26.     Android:id="@+id/bt_ok"  
    27.     Android:text="确定"  
    28.     Android:layout_width="100px"    
    29.     Android:layout_height="50px"  
    30.     />     
    31.   
    32. <Button  
    33.     Android:id="@+id/bt_cancle"  
    34.     Android:text="取消"  
    35.     Android:layout_width="100px"    
    36.     Android:layout_height="50px"  
    37.     />     
    38. </LinearLayout>  
    39. </LinearLayout>  

           代码:      

     
    1. package com.myAndroid.test;   
    2.   
    3. import Android.app.Activity;   
    4. import Android.content.Context;   
    5. import Android.content.SharedPreferences.Editor;   
    6. import Android.os.Bundle;   
    7. import Android.util.Log;   
    8. import Android.view.Gravity;   
    9. import Android.view.LayoutInflater;   
    10. import Android.view.View;   
    11. import Android.view.View.OnClickListener;   
    12. import Android.view.ViewGroup.LayoutParams;   
    13. import Android.widget.Button;   
    14. import Android.widget.EditText;   
    15. import Android.widget.Gallery;   
    16. import Android.widget.PopupWindow;   
    17. import Android.widget.TextView;   
    18.   
    19. public class PopupWindowTest extends Activity {//PopupWindow属于不阻塞的对话框,AlertDialog则是阻塞的。   
    20.     private Button bt_popupWindow1;   
    21.     private Button bt_popupWindow2;   
    22.     private Button bt_popupWindow3;   
    23.     private Button bt_popupWindow4;   
    24.     private TextView tv_showText;   
    25.     private PopupWindow popupWindow;   
    26.     private int screenWidth;   
    27.     private int screenHeight;   
    28.     private int dialgoWidth;   
    29.     private int dialgoheight;   
    30.        
    31.   
    32.     /** Called when the activity is first created. */  
    33.     @Override  
    34.     public void onCreate(Bundle savedInstanceState) {   
    35.         super.onCreate(savedInstanceState);   
    36.         setContentView(R.layout.main);   
    37.            
    38.         initView();   
    39.     }   
    40.        
    41.     /**  
    42.      * 初始化控件和响应事件  
    43.      */  
    44.     private void initView() {   
    45.         bt_popupWindow1 = (Button)findViewById(R.id.bt_PopupWindow1);   
    46.         bt_popupWindow2 = (Button)findViewById(R.id.bt_PopupWindow2);   
    47.         bt_popupWindow3 = (Button)findViewById(R.id.bt_PopupWindow3);   
    48.         bt_popupWindow4 = (Button)findViewById(R.id.bt_PopupWindow4);   
    49.         tv_showText = (TextView)findViewById(R.id.tv_showText);   
    50.            
    51.         bt_popupWindow1.setOnClickListener(new ClickEvent());   
    52.         bt_popupWindow2.setOnClickListener(new ClickEvent());   
    53.         bt_popupWindow3.setOnClickListener(new ClickEvent());   
    54.         bt_popupWindow4.setOnClickListener(new ClickEvent());   
    55.            
    56.            
    57.            
    58.     }   
    59.        
    60.     /**  
    61.      * 按钮点击事件处理  
    62.      * @author Kobi  
    63.      *  
    64.      */  
    65.     private class ClickEvent implements OnClickListener {   
    66.            
    67.         @Override  
    68.         public void onClick(View v) {   
    69.             // TODO Auto-generated method stub   
    70.             switch(v.getId()) {   
    71.                    
    72.             case R.id.bt_PopupWindow1:  //以自己为Anchor,不偏移   
    73.                 getPopupWindow();   
    74.                 popupWindow.showAsDropDown(v);   
    75.                 break;   
    76.                    
    77.             case R.id.bt_PopupWindow2:  //以自己为Anchor,偏移(screenWidth-dialgoWidth)/2, 0)--按钮正下方   
    78.                 getPopupWindow();   
    79.                 popupWindow.showAsDropDown(v, (screenWidth-dialgoWidth)/20);   
    80.                 break;   
    81.                    
    82.             case R.id.bt_PopupWindow3:  //以屏幕中心为参照,不偏移   
    83.                 getPopupWindow();   
    84.                 popupWindow.showAtLocation(findViewById(R.id.layout), Gravity.CENTER, 00);   
    85.                 break;   
    86.                    
    87.             case R.id.bt_PopupWindow4:  //以屏幕左下角为参照,偏移(screenWidth-dialgoWidth)/2, 0) --屏幕下方中央   
    88.                 getPopupWindow();   
    89.                 popupWindow.showAtLocation(findViewById(R.id.layout),    
    90.                         Gravity.BOTTOM, 00);   
    91.                 break;   
    92.                    
    93.             default:   
    94.                 break;   
    95.             }   
    96.         }   
    97.            
    98.     }   
    99.   
    100.     /**  
    101.      * 创建PopupWindow  
    102.      */  
    103.     protected void initPopuptWindow() {   
    104.         // TODO Auto-generated method stub   
    105.            
    106.            
    107.         View popupWindow_view = getLayoutInflater().inflate(    //获取自定义布局文件dialog.xml的视图   
    108.                 R.layout.dialog, null,false);   
    109.            
    110.         popupWindow = new PopupWindow(popupWindow_view, 200150true);//创建PopupWindow实例  
    111.            
    112.         Button bt_ok = (Button)popupWindow_view.findViewById(R.id.bt_ok);   //dialog.xml视图里面的控件   
    113.         Button bt_cancle = (Button)popupWindow_view.findViewById(R.id.bt_cancle);   
    114.         final EditText et_text = (EditText)popupWindow_view.findViewById(R.id.et_text);    
    115.            
    116.         bt_ok.setOnClickListener(new OnClickListener() {   
    117.                
    118.             @Override  
    119.             public void onClick(View v) {   
    120.                 // TODO Auto-generated method stub   
    121.                 tv_showText.setText(et_text.getText()); //在标签里显示内容   
    122.                 popupWindow.dismiss();                  //对话框消失   
    123.             }   
    124.         });   
    125.            
    126.         bt_cancle.setOnClickListener(new OnClickListener() {   
    127.                
    128.             @Override  
    129.             public void onClick(View v) {   
    130.                 // TODO Auto-generated method stub   
    131.                 popupWindow.dismiss();   
    132.             }   
    133.         });   
    134.            
    135.         //获取屏幕和对话框各自高宽   
    136.         screenWidth = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getWidth();   
    137.         screenHeight = PopupWindowTest.this.getWindowManager().getDefaultDisplay().getHeight();   
    138.   
    139.         dialgoWidth = popupWindow.getWidth();   
    140.         dialgoheight = popupWindow.getHeight();   
    141.            
    142.     }   
    143.        
    144.     /*  
    145.      * 获取PopupWindow实例  
    146.      */  
    147.     private void getPopupWindow() {   
    148.            
    149.         if(null != popupWindow) {   
    150.             popupWindow.dismiss();   
    151.             return;   
    152.         }else {   
    153.             initPopuptWindow();   
    154.         }   
    155.     }   
    156.   
    157.     @Override  
    158.     protected void onPause() {   
    159.         // TODO Auto-generated method stub   
    160.         super.onPause();   
    161.         Log.e("ActivityState""onPause");   
    162.     }   
    163.   
    164.     @Override  
    165.     protected void onResume() {   
    166.         // TODO Auto-generated method stub   
    167.         super.onResume();   
    168.         Log.e("ActivityState""onResume");   
    169.     }   
    170.        
    171.        
    172.   
    173.        
    174. }  


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值