PopupWindow的简单运用

PopupWindow可实现弹出窗口效果。

PopupWindow与Dialog的区别是:

1.PopupWindow是阻塞式的,Dialog是非阻塞式的。

2.PopupWindow弹出时没有背景,Dialog弹出时有背景。

 

下面是一个简单的例子。

activity类:

[java] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. package com.zzj.ui.popupwindow;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.ColorDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.widget.AdapterView;  
  9. import android.widget.AdapterView.OnItemClickListener;  
  10. import android.widget.ArrayAdapter;  
  11. import android.widget.ListView;  
  12. import android.widget.PopupWindow;  
  13. import android.widget.TextView;  
  14. import android.widget.Toast;  
  15.   
  16. import com.zzj.ui.R;  
  17.   
  18. public class PopubWindowActivity extends Activity {  
  19.   
  20.     private PopupWindow popupWindow;  
  21.   
  22.     @Override  
  23.     protected void onCreate(Bundle savedInstanceState) {  
  24.         super.onCreate(savedInstanceState);  
  25.   
  26.         setContentView(R.layout.popupwindow_activity);  
  27.         initPopupwindow();  
  28.     }  
  29.   
  30.     private void initPopupwindow() {  
  31.         LayoutInflater inflater = LayoutInflater.from(this);  
  32.         View popupwindowView = inflater  
  33.                 .inflate(R.layout.popupwindow_menu, null);  
  34.         ListView listView = (ListView) popupwindowView  
  35.                 .findViewById(R.id.listView1);  
  36.         listView.setAdapter(new ArrayAdapter<String>(this,  
  37.                 R.layout.popupwindow_menu_item,  
  38.                 new String[] { "复制", "修改", "删除" }));  
  39.         listView.setOnItemClickListener(new OnItemClickListener() {  
  40.   
  41.             @Override  
  42.             public void onItemClick(AdapterView<?> parent, View view,  
  43.                     int position, long id) {  
  44.                 Toast.makeText(PopubWindowActivity.this,  
  45.                         ((TextView) view).getText(), Toast.LENGTH_LONG).show();  
  46.             }  
  47.         });  
  48.   
  49.         popupWindow = new PopupWindow();  
  50.         popupWindow.setContentView(popupwindowView);  
  51.         popupWindow.setFocusable(true);// 很重要  
  52.         ColorDrawable background = new ColorDrawable(0x000000);  
  53.         popupWindow.setBackgroundDrawable(background);// 很重要  
  54.         popupWindow.setAnimationStyle(R.style.PopupwindowAnimation);// 弹出动画  
  55.         popupWindow.setWidth(100);  
  56.         popupWindow.setHeight(100);  
  57.     }  
  58.   
  59.     public void popupwindow(View v) {  
  60.         if (!popupWindow.isShowing()) {  
  61.             popupWindow.showAsDropDown(v, 5, 5);  
  62.         }  
  63.     }  
  64. }  

有两个非常重要的两个属性focusable和background需要设置。如果popupwindow没有得到焦点,则任何在popupwindow上的点击事件都无效。如果没有给popupwindow设置背景,当popupwindow获取焦点后,back键将无效,并且点击popupwindow之外的范围也无效;如果给popupwindow设置了背景,点击back键或者点击popupwindow之外的范围,popupwindow都将关闭。

 

activity布局文件:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/button1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:onClick="popupwindow"  
  12.         android:text="popupwindow" />  
  13.   
  14.     <Button  
  15.         android:id="@+id/button2"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="点得到我吗" />  
  19.   
  20. </LinearLayout>  

popupwindow布局文件:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:background="@color/black"  
  6.     android:orientation="vertical" >  
  7.   
  8.     <ListView  
  9.         android:id="@+id/listView1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content" >  
  12.     </ListView>  
  13.   
  14. </LinearLayout>  

列表项(菜单项)布局文件:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TextView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:textColor="@color/white" >  
  6.   
  7. </TextView>  

样式:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <!-- popupwindow弹出动画 -->  
  2.     <style name="PopupwindowAnimation">  
  3.         <item name="android:windowEnterAnimation">@anim/fade_in</item>  
  4.         <item name="android:windowExitAnimation">@anim/fade_out</item>  
  5.     </style>  

anim/fade_in动画:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fromXScale="0.0"  
  7.         android:fromYScale="0.0"  
  8.         android:toXScale="1.0"  
  9.         android:toYScale="1.0" />  
  10.   
  11. </set>  

anim/fade_out动画:

[html] view plain copy
 
 print?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <scale  
  5.         android:duration="500"  
  6.         android:fromXScale="1.0"  
  7.         android:fromYScale="1.0"  
  8.         android:toXScale="0.0"  
  9.         android:toYScale="0.0" />  
  10.   
  11. </set>  

效果:

 

posted @ 2016-11-28 17:48  天涯海角路  阅读(74)  评论(0)    收藏  举报