zoukankan      html  css  js  c++  java
  • Android中的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

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

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

    PopupWindowDemoActivity.java
    [java] view plaincopy
     
    1. package com.tianjf;  
    2.   
    3. import android.app.Activity;  
    4. import android.os.Bundle;  
    5. import android.view.Gravity;  
    6. import android.view.LayoutInflater;  
    7. import android.view.View;  
    8. import android.view.View.OnClickListener;  
    9. import android.widget.Button;  
    10. import android.widget.PopupWindow;  
    11. import android.widget.RadioGroup;  
    12. import android.widget.RadioGroup.OnCheckedChangeListener;  
    13.   
    14. public class PopupWindowDemoActivity extends Activity implements OnClickListener,  
    15.         OnCheckedChangeListener {  
    16.   
    17.     private Button mbutton01;  
    18.     private Button mbutton02;  
    19.     private Button mbutton03;  
    20.     private Button mbutton04;  
    21.     private PopupWindow mPopupWindow;  
    22.     // 屏幕的width  
    23.     private int mScreenWidth;  
    24.     // 屏幕的height  
    25.     private int mScreenHeight;  
    26.     // PopupWindow的width  
    27.     private int mPopupWindowWidth;  
    28.     // PopupWindow的height  
    29.     private int mPopupWindowHeight;  
    30.   
    31.     @Override  
    32.     public void onCreate(Bundle savedInstanceState) {  
    33.         super.onCreate(savedInstanceState);  
    34.         setContentView(R.layout.main);  
    35.   
    36.         mbutton01 = (Button) findViewById(R.id.button01);  
    37.         mbutton02 = (Button) findViewById(R.id.button02);  
    38.         mbutton03 = (Button) findViewById(R.id.button03);  
    39.         mbutton04 = (Button) findViewById(R.id.button04);  
    40.   
    41.         mbutton01.setOnClickListener(this);  
    42.         mbutton02.setOnClickListener(this);  
    43.         mbutton03.setOnClickListener(this);  
    44.         mbutton04.setOnClickListener(this);  
    45.     }  
    46.   
    47.     @Override  
    48.     public void onClick(View v) {  
    49.         switch (v.getId()) {  
    50.         // 相对某个控件的位置(正左下方),无偏移  
    51.         case R.id.button01:  
    52.             getPopupWindowInstance();  
    53.             mPopupWindow.showAsDropDown(v);  
    54.             break;  
    55.   
    56.         // 相对某个控件的位置(正左下方),有偏移  
    57.         case R.id.button02:  
    58.             getPopupWindowInstance();  
    59.             mPopupWindow.showAsDropDown(v, 50, 50);// X、Y方向各偏移50  
    60.             break;  
    61.   
    62.         // 相对于父控件的位置,无偏移  
    63.         case R.id.button03:  
    64.             getPopupWindowInstance();  
    65.             mPopupWindow.showAtLocation(v, Gravity.CENTER, 0, 0);  
    66.             break;  
    67.   
    68.         // 相对于父控件的位置,有偏移  
    69.         case R.id.button04:  
    70.             getPopupWindowInstance();  
    71.             mPopupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 50);  
    72.             break;  
    73.   
    74.         default:  
    75.             break;  
    76.         }  
    77.     }  
    78.   
    79.     @Override  
    80.     public void onCheckedChanged(RadioGroup group, int checkedId) {  
    81.         mPopupWindow.dismiss();  
    82.     }  
    83.   
    84.     /* 
    85.      * 获取PopupWindow实例 
    86.      */  
    87.     private void getPopupWindowInstance() {  
    88.         if (null != mPopupWindow) {  
    89.             mPopupWindow.dismiss();  
    90.             return;  
    91.         } else {  
    92.             initPopuptWindow();  
    93.         }  
    94.     }  
    95.   
    96.     /* 
    97.      * 创建PopupWindow 
    98.      */  
    99.     private void initPopuptWindow() {  
    100.         LayoutInflater layoutInflater = LayoutInflater.from(this);  
    101.         View popupWindow = layoutInflater.inflate(R.layout.popup_window, null);  
    102.         RadioGroup radioGroup = (RadioGroup) popupWindow.findViewById(R.id.radioGroup);  
    103.         radioGroup.setOnCheckedChangeListener(this);  
    104.   
    105.         // 创建一个PopupWindow  
    106.         // 参数1:contentView 指定PopupWindow的内容  
    107.         // 参数2:width 指定PopupWindow的width  
    108.         // 参数3:height 指定PopupWindow的height  
    109.         mPopupWindow = new PopupWindow(popupWindow, 100, 130);  
    110.   
    111.         // 获取屏幕和PopupWindow的width和height  
    112.         mScreenWidth = getWindowManager().getDefaultDisplay().getWidth();  
    113.         mScreenWidth = getWindowManager().getDefaultDisplay().getHeight();  
    114.         mPopupWindowWidth = mPopupWindow.getWidth();  
    115.         mPopupWindowHeight = mPopupWindow.getHeight();  
    116.     }  
    117. }  
     
  • 相关阅读:
    eclipse如何与git 配合工作。
    git托管代码(二)
    PPC2003 安装 CFNET 3.5成功
    我的Window Mobile WCF 項目 第三篇 WM窗体设计
    我的Window Mobile WCF 項目 第一篇Mobile开发和WinForm开发的区别
    我的Window Mobile WCF 項目 第七天
    我的Window Mobile WCF 項目 第二篇 WindowsMobile访问WCF
    WCF 用vs2010 和 vs2008的简单对比测试
    vs2010beta1 和 搜狗输入法 冲突,按下 Ctrl 键就报错,重装搜狗解决
    我的Window Mobile WCF 項目 第六天 (二)
  • 原文地址:https://www.cnblogs.com/xgjblog/p/4158775.html
Copyright © 2011-2022 走看看