PopUpWindow使用詳解(一)——基本使用

相關文章:

1、《PopUpWindow使用詳解(一)——基本使用》

2、《PopUpWindow使用詳解(二)——進階及答疑》


有同學講到想要知道PopUpWindow的知識,這裏就給大家講一講PopUpWindow的基本用法和原理吧。這段時間博客可能會更新比較慢,因爲你懂的 !!-_- ,往左看公告,嘿嘿。

先看一下我們要做的效果:


這個效果很容易理解:當點擊btn時,在底部彈出PopupWindow,然後點擊各個item彈出對應toast。

一、概述

1、PopupWindow與AlertDialog的區別

最關鍵的區別是AlertDialog不能指定顯示位置,只能默認顯示在屏幕最中間(當然也可以通過設置WindowManager參數來改變位置)。而PopupWindow是可以指定顯示位置的,隨便哪個位置都可以,更加靈活。
有關Dialog的相關知識,大家可以參考我的系列博客:《詳解Dialog(一)——基礎元素構建》

2、PopupWindow的相關函數

(1)、構造函數:

  1. //方法一:  
  2. public PopupWindow (Context context)  
  3. //方法二:  
  4. public PopupWindow(View contentView)  
  5. //方法三:  
  6. public PopupWindow(View contentView, int width, int height)  
  7. //方法四:  
  8. public PopupWindow(View contentView, int width, int height, boolean focusable)  
首要注意:看這裏有四個構造函數,但要生成一個PopupWindow最基本的三個條件是一定要設置的:View contentView,int width, int height ;少任意一個就不可能彈出來PopupWindow!!!!
所以,如果使用方法一來構造PopupWindow,那完整的構造代碼應該是這樣的:
  1. View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  2. PopupWindwo popWnd = PopupWindow (context);  
  3. popWnd.setContentView(contentView);  
  4. popWnd.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);  
  5. popWnd.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);  
有關爲什麼一定要設置width和height的原因,我們後面會講,這裏說一下爲什麼樣強制設置contentView;很簡單的原因是因爲PopupWindow沒有默認佈局,它不會像AlertDialog那樣只setTitle,就能彈出來一個框。PopupWindow是沒有默認佈局的,它的佈局只有通過我們自己設置才行。由於方法三中,含有了這三個必備條件,不用單獨設置contentview或者width、height,所以構造方法三是用的最多的一個構造方法。
最後,方法四中的focusable變量不是必須的,有關它的方法和意義,我們會在下一篇中細講。

(2)顯示函數

顯示函數主要使用下面三個:

  1. //相對某個控件的位置(正左下方),無偏移  
  2. showAsDropDown(View anchor):  
  3. //相對某個控件的位置,有偏移;xoff表示x軸的偏移,正值表示向左,負值表示向右;yoff表示相對y軸的偏移,正值是向下,負值是向上;  
  4. showAsDropDown(View anchor, int xoff, int yoff):  
  5. //相對於父控件的位置(例如正中央Gravity.CENTER,下方Gravity.BOTTOM等),可以設置偏移或無偏移  
  6. showAtLocation(View parent, int gravity, int x, int y):  
這裏有兩種顯示方式:
1、顯示在某個指定控件的下方
showAsDropDown(View anchor):
showAsDropDown(View anchor, int xoff, int yoff);
2、指定父視圖,顯示在父控件的某個位置(Gravity.TOP,Gravity.RIGHT等)
showAtLocation(View parent, int gravity, int x, int y);

(3)、其它函數

  1. public void dismiss()  
  2. //另外幾個函數,這裏不講其意義,下篇細講  
  3. public void setFocusable(boolean focusable)  
  4. public void setTouchable(boolean touchable)  
  5. public void setOutsideTouchable(boolean touchable)  
  6. public void setBackgroundDrawable(Drawable background)  
這幾個函數裏,這篇只會用到dismiss(),用於不需要的時候,將窗體隱藏掉。

好了,廢話不多說了,我們就做一個上面的例子來看一下。

二、簡單示例(showAtLocation顯示窗體)

在這個例子中,我們實現兩個功能,彈出popupWindow和Item點擊響應

1、主佈局(main.xml)

從效果圖中也可以看到主佈局只有一個button,什麼都沒有,所以它的佈局代碼哪下:

  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.     <Button  
  7.             android:id="@+id/btn"  
  8.             android:layout_width="fill_parent"  
  9.             android:layout_height="wrap_content"  
  10.             android:text="show popWindow"/>  
  11. </LinearLayout>  

2、PopupWindow佈局(popuplayout.xml)


在概述中,我們提到了,必須爲PopupWindow設置佈局,從效果圖中,我也可以看到它的佈局有三個item,中間用橫線分開。所以這裏佈局使用Listview應該更合適,但爲了減輕代碼難度,我們直接使用TextView和分隔線來代替,代碼如下:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.         xmlns:android="http://schemas.android.com/apk/res/android"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content"  
  7.         android:background="#ffffff"  
  8.         android:orientation="vertical"  
  9.         android:paddingBottom="2dp">  
  10.     <View  
  11.             android:layout_width="match_parent"  
  12.             android:layout_height="2.25dp"  
  13.             android:background="#fa7829"  
  14.             android:layout_alignParentTop="true"/>  
  15.   
  16.     <TextView  
  17.             android:id="@+id/pop_computer"  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="wrap_content"  
  20.             style="@style/pop_text_style"  
  21.             android:text="計算機"/>  
  22.   
  23.     <View  
  24.             android:layout_width="match_parent"  
  25.             android:layout_height="1dp"  
  26.             android:background="@drawable/list_line"/>  
  27.   
  28.     <TextView  
  29.             android:id="@+id/pop_financial"  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="wrap_content"  
  32.             style="@style/pop_text_style"  
  33.             android:text="金融"/>  
  34.   
  35.     <View  
  36.             android:layout_width="match_parent"  
  37.             android:layout_height="1dp"  
  38.             android:background="@drawable/list_line"/>  
  39.   
  40.     <TextView  
  41.             android:id="@+id/pop_manage"  
  42.             android:layout_width="match_parent"  
  43.             android:layout_height="wrap_content"  
  44.             style="@style/pop_text_style"  
  45.             android:text="管理"/>  
  46.   
  47.     <View  
  48.             android:layout_width="match_parent"  
  49.             android:layout_height="1dp"/>  
  50.   
  51. </LinearLayout>  

3、MainActivity代碼

先貼出來完整代碼,然後再逐步講解:
  1. public class MainActivity extends Activity implements View.OnClickListener{  
  2.   
  3.     private PopupWindow mPopWindow;  
  4.   
  5.     @Override  
  6.     public void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.main);  
  9.   
  10.         Button btn = (Button) findViewById(R.id.btn);  
  11.         btn.setOnClickListener(new View.OnClickListener() {  
  12.             @Override  
  13.             public void onClick(View v) {  
  14.                 showPopupWindow();  
  15.             }  
  16.         });  
  17.     }  
  18.   
  19.     private void showPopupWindow() {  
  20.         //設置contentView  
  21.         View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  22.         mPopWindow = new PopupWindow(contentView,  
  23.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);  
  24.         mPopWindow.setContentView(contentView);  
  25.         //設置各個控件的點擊響應  
  26.         TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);  
  27.         TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);  
  28.         TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);  
  29.         tv1.setOnClickListener(this);  
  30.         tv2.setOnClickListener(this);  
  31.         tv3.setOnClickListener(this);  
  32.         //顯示PopupWindow  
  33.         View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);  
  34.         mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 00);  
  35.   
  36.     }  
  37.   
  38.     @Override  
  39.     public void onClick(View v) {  
  40.         int id = v.getId();  
  41.         switch (id){  
  42.             case R.id.pop_computer:{  
  43.                 Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show();  
  44.                 mPopWindow.dismiss();  
  45.             }  
  46.             break;  
  47.             case R.id.pop_financial:{  
  48.                 Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();  
  49.                 mPopWindow.dismiss();  
  50.             }  
  51.             break;  
  52.             case R.id.pop_manage:{  
  53.                 Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();  
  54.                 mPopWindow.dismiss();  
  55.             }  
  56.             break;  
  57.         }  
  58.     }  
  59. }  

(1)首先看OnCreate()

在OnCreate()中只做了一個操作,當點擊Button時,顯示窗體:
  1. public void onCreate(Bundle savedInstanceState) {  
  2.     super.onCreate(savedInstanceState);  
  3.     setContentView(R.layout.main);  
  4.   
  5.     Button btn = (Button) findViewById(R.id.btn);  
  6.     btn.setOnClickListener(new View.OnClickListener() {  
  7.         @Override  
  8.         public void onClick(View v) {  
  9.             showPopupWindow();  
  10.         }  
  11.     });  
  12. }  

(2)、顯示PopupWindow

下面是有關窗體操作的代碼:
  1. private void showPopupWindow() {  
  2.      //設置contentView  
  3.     View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  4.     mPopWindow = new PopupWindow(contentView,  
  5.             LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);  
  6.  //設置各個控件的點擊響應  
  7.     TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);  
  8.     TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);  
  9.     TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);  
  10.     tv1.setOnClickListener(this);  
  11.     tv2.setOnClickListener(this);  
  12.     tv3.setOnClickListener(this);  
  13.     //顯示PopupWindow  
  14.     View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);  
  15.     mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 00);  
  16.   
  17. }  
這裏同樣分爲三部分:
第一部分:設置ContentView
  1. View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  2. mPopWindow = new PopupWindow(contentView,LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);  
利用LayoutInflater獲取R.layout.popuplayout對應的View,然後利用我們上面所講的構造函數三來生成mPopWindow;
這裏 要注意一個問題:在這個構造函數裏,我們傳進去了width和height全部都是WRAP_CONTENT;而在R.layout.popuplayou的根佈局中,我們定義的width和height代碼是:layout_width="fill_parent",layout_height="wrap_content";原代碼如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.   
  3. <LinearLayout  
  4.         xmlns:android="http://schemas.android.com/apk/res/android"  
  5.         android:layout_width="fill_parent"  
  6.         android:layout_height="wrap_content"  
  7.         android:background="#ffffff"  
  8.         android:orientation="vertical"  
  9.         android:paddingBottom="2dp">  
  10.           
  11.         ………………  
  12. </LinearLayout>          
這裏就有衝突了,那顯示出來的popupWindow是按哪個來的呢?
從效果圖中來看,明顯PopupWindow寬度並沒有全屏,顯然是按代碼中的佈局爲準。
這說明了:
**如果在代碼中重新設置了popupWindow的寬和高,那就以代碼中所設置爲準。**(至於原因,下篇會講)

第二部分:設置各個控件的點擊響應
  1. TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);  
  2. TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);  
  3. TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);  
  4. tv1.setOnClickListener(this);  
  5. tv2.setOnClickListener(this);  
  6. tv3.setOnClickListener(this);  
這部分沒什麼好講了,設置PopupWindow中各個控件的點擊響應,但一定要注意的是,PopupWindow中各個控件的所在的佈局是contentView,而不是在Activity中,如果大家直接使用
  1. TextView tv1 = (TextView)findViewById(R.id.pop_computer);  
肯定會報錯,因爲R.id.pop_computer這個ID值在當前Activtiy的佈局文件中是找不到的。只有在R.layout.popuplayout的佈局文件中纔會有!所以,這就是爲什麼,要在findViewById(R.id.pop_computer)前指定contentView的原因!!!在實際項目中,很容易遇到像這種需要指定根佈局的情況,大家需要注意。
有關響應,就沒什麼好講的了,因爲我們在類頂部派生了View.OnClickListener,所以在OnClick函數中,直接處理即可,代碼如下:(在點擊不同的Item時,一邊彈出不同的toast,一邊將PopupWindow隱藏掉)
  1. @Override  
  2. public void onClick(View v) {  
  3.     int id = v.getId();  
  4.     switch (id){  
  5.         case R.id.pop_computer:{  
  6.             Toast.makeText(this,"clicked computer",Toast.LENGTH_SHORT).show();  
  7.             mPopWindow.dismiss();  
  8.         }  
  9.         break;  
  10.         case R.id.pop_financial:{  
  11.             Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();  
  12.             mPopWindow.dismiss();  
  13.         }  
  14.         break;  
  15.         case R.id.pop_manage:{  
  16.             Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();  
  17.             mPopWindow.dismiss();  
  18.         }  
  19.         break;  
  20.     }  
  21. }  
第三部分:showAtLocation顯示窗體
  1. View rootview = LayoutInflater.from(MainActivity.this).inflate(R.layout.main, null);  
  2. mPopWindow.showAtLocation(rootview, Gravity.BOTTOM, 00);  
showAtLocation的顯示就將PopupWindow的實例放在一個父容器中,然後指定顯示在父容器中的位置。
由於,我們要將mPopWindow放在整個屏幕的最低部,所以我們將R.layout.main做爲它的父容器。將其顯示在BOTTOM的位置。

到這裏,有關PopupWindow的顯示及其中控件響應基本都講完了,下面,我們就講講showAsDropDown顯示窗體的用法。


源碼在文章底部給出

三、另一示例(showAsDropDown顯示窗體)

大家先看下面這個效果圖:

這個效果圖演示的是,在點擊標題欄右方的“菜單”按鈕時,在其下方顯示一個自定義的菜單列表。

1、同樣,我們先看看主佈局代碼(main.xml)

  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.     <RelativeLayout android:layout_width="fill_parent"  
  8.                     android:layout_height="wrap_content"  
  9.                     android:background="#ffffff">  
  10.   
  11.         <TextView android:layout_width="wrap_content"  
  12.                   android:layout_height="wrap_content"  
  13.                   android:layout_alignParentLeft="true"  
  14.                   android:textColor="#50484b"  
  15.                   android:padding="10dp"  
  16.                   android:text="返回"/>  
  17.   
  18.         <TextView  
  19.                 android:id="@+id/menu"  
  20.                 android:layout_width="wrap_content"  
  21.                 android:layout_height="wrap_content"  
  22.                 android:layout_alignParentRight="true"  
  23.                 android:textColor="#50484b"  
  24.                 android:padding="10dp"  
  25.                 android:text="菜單"/>  
  26.   
  27.     </RelativeLayout>  
  28. </LinearLayout>  
這段代碼的佈局很簡單,就是生成一個標題欄,上面有兩個按鈕,“返回”和“菜單”

2、PopupWindow佈局代碼(popuplayout.xml)

這部分佈局也不難,只得利用純代碼硬生成一個列表的佈局。在實際項目中,大家應該使用listview來動態生成列表,這樣生成的popupWindow就是可以複用的了。有關佈局就不再多講,跟上面的佈局基本一樣,只是換了背景。
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="wrap_content"  
  6.         android:background="@drawable/pop_bg"  
  7.         android:orientation="vertical"  
  8.         android:paddingBottom="2dp">  
  9.   
  10.     <TextView  
  11.             android:id="@+id/pop_computer"  
  12.             android:layout_width="wrap_content"  
  13.             android:layout_height="wrap_content"  
  14.             style="@style/pop_text_style"  
  15.             android:text="計算機"/>  
  16.   
  17.     <View  
  18.             android:layout_width="match_parent"  
  19.             android:layout_height="1dp"  
  20.             android:background="@drawable/list_line"/>  
  21.   
  22.     <TextView  
  23.             android:id="@+id/pop_financial"  
  24.             android:layout_width="wrap_content"  
  25.             android:layout_height="wrap_content"  
  26.             style="@style/pop_text_style"  
  27.             android:text="金融"/>  
  28.   
  29.     <View  
  30.             android:layout_width="match_parent"  
  31.             android:layout_height="1dp"  
  32.             android:background="@drawable/list_line"/>  
  33.   
  34.     <TextView  
  35.             android:id="@+id/pop_manage"  
  36.             android:layout_width="wrap_content"  
  37.             android:layout_height="wrap_content"  
  38.             style="@style/pop_text_style"  
  39.             android:text="管理"/>  
  40.   
  41.     <View  
  42.             android:layout_width="match_parent"  
  43.             android:layout_height="1dp"/>  
  44.   
  45. </LinearLayout>  

3、MainActivity代碼

同樣是先貼出來完整代碼,然後再細講。
  1. public class MainActivity extends Activity implements View.OnClickListener{  
  2.     private PopupWindow mPopWindow;  
  3.     private TextView mMenuTv;  
  4.     @Override  
  5.     public void onCreate(Bundle savedInstanceState) {  
  6.         super.onCreate(savedInstanceState);  
  7.         setContentView(R.layout.main);  
  8.         mMenuTv = (TextView)findViewById(R.id.menu);  
  9.         mMenuTv.setOnClickListener(new View.OnClickListener() {  
  10.             @Override  
  11.             public void onClick(View v) {  
  12.                 showPopupWindow();  
  13.             }  
  14.         });  
  15.     }  
  16.     private void showPopupWindow() {  
  17.         View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  18.         mPopWindow = new PopupWindow(contentView);  
  19.         mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);  
  20.         mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);  
  21.   
  22.         TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);  
  23.         TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);  
  24.         TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);  
  25.         tv1.setOnClickListener(this);  
  26.         tv2.setOnClickListener(this);  
  27.         tv3.setOnClickListener(this);  
  28.   
  29.         mPopWindow.showAsDropDown(mMenuTv);  
  30.     }  
  31.   
  32.     @Override  
  33.     public void onClick(View v) {  
  34.         int id = v.getId();  
  35.         switch (id){  
  36.             case R.id.pop_computer:{  
  37.                 Toast.makeText(this, "clicked computer", Toast.LENGTH_SHORT).show();  
  38.                 mPopWindow.dismiss();  
  39.             }  
  40.             break;  
  41.             case R.id.pop_financial:{  
  42.                 Toast.makeText(this,"clicked financial",Toast.LENGTH_SHORT).show();  
  43.                 mPopWindow.dismiss();  
  44.             }  
  45.             break;  
  46.             case R.id.pop_manage:{  
  47.                 Toast.makeText(this,"clicked manage",Toast.LENGTH_SHORT).show();  
  48.                 mPopWindow.dismiss();  
  49.             }  
  50.             break;  
  51.         }  
  52.     }  
  53. }  
這段代碼的意義就是點擊menu彈出popupwindow,然後對各個item進行響應,我們主要講講showPopupWindow() 這部分,對於item響應的部分與上個示例都一樣,就不再細講。
  1. private void showPopupWindow() {  
  2.     View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  3.     mPopWindow = new PopupWindow(contentView);  
  4.     mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);  
  5.     mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);  
  6.   
  7.     TextView tv1 = (TextView)contentView.findViewById(R.id.pop_computer);  
  8.     TextView tv2 = (TextView)contentView.findViewById(R.id.pop_financial);  
  9.     TextView tv3 = (TextView)contentView.findViewById(R.id.pop_manage);  
  10.     tv1.setOnClickListener(this);  
  11.     tv2.setOnClickListener(this);  
  12.     tv3.setOnClickListener(this);  
  13.   
  14.     mPopWindow.showAsDropDown(mMenuTv);  
  15. }  
這裏首先注意的一個地方,在這個示例中,我們是使用的構造方法二來生成的PopupWindow實例的,同樣,再強調一遍:contentView,Width,Height這三個元素是必須設置的,缺一不可!至於爲什麼要顯式設置Width,Height,我們下篇會講到。
  1. View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  2. mPopWindow = new PopupWindow(contentView);  
  3. mPopWindow.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);  
  4. mPopWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);  
然後就是使用showAsDropDown()顯示PopupWindow:
  1. mPopWindow.showAsDropDown(mMenuTv);  
大家也現樣可以使用showAsDropDown(View anchor, int xoff, int yoff):來添加相對x軸和y軸的位移量。具體用法就不再細講,沒什麼難度,大家試一試即可。
好了,這部分示例也講完了,下面我們就在這個示例上升級一個功能:講講怎麼在彈出PopupWindow的同時利用陰影把背景全部給遮罩起來。
源碼在文章底部給出

四、提高:爲菜單添加陰影

這部分的效果圖是下面這樣的:

從效果圖中可以看出,這部分是上一個示例的升級版,就是在點出PopupWindow的同時,把背景用半透明黑色遮罩住,像彈出AlertDialog一樣的效果。下面就來看看這個效果是怎麼實現的吧。

1、PopupWindow佈局(popuplayout.xml)

其實原理很簡單,使PopupWindow的界面充滿全屏,而實際的列表菜單只是其中的一個子佈局即可,所以此時的PopupWindow的佈局代碼如下:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.         xmlns:android="http://schemas.android.com/apk/res/android"  
  4.         android:layout_width="fill_parent"  
  5.         android:layout_height="fill_parent"  
  6.         android:background="#66000000">  
  7.     <LinearLayout  
  8.             android:layout_width="wrap_content"  
  9.             android:layout_height="wrap_content"  
  10.             android:background="@drawable/pop_bg"  
  11.             android:orientation="vertical"  
  12.             android:paddingBottom="2dp"  
  13.             android:layout_alignParentRight="true">  
  14.   
  15.         <TextView  
  16.                 android:id="@+id/pop_computer"  
  17.                 android:layout_width="wrap_content"  
  18.                 android:layout_height="wrap_content"  
  19.                 style="@style/pop_text_style"  
  20.                 android:text="計算機"/>  
  21.   
  22.         <View  
  23.                 android:layout_width="match_parent"  
  24.                 android:layout_height="1dp"  
  25.                 android:background="@drawable/list_line"/>  
  26.   
  27.         <TextView  
  28.                 android:id="@+id/pop_financial"  
  29.                 android:layout_width="wrap_content"  
  30.                 android:layout_height="wrap_content"  
  31.                 style="@style/pop_text_style"  
  32.                 android:text="金融"/>  
  33.   
  34.         <View  
  35.                 android:layout_width="match_parent"  
  36.                 android:layout_height="1dp"  
  37.                 android:background="@drawable/list_line"/>  
  38.   
  39.         <TextView  
  40.                 android:id="@+id/pop_manage"  
  41.                 android:layout_width="wrap_content"  
  42.                 android:layout_height="wrap_content"  
  43.                 style="@style/pop_text_style"  
  44.                 android:text="管理"/>  
  45.   
  46.         <View  
  47.                 android:layout_width="match_parent"  
  48.                 android:layout_height="1dp"/>  
  49.   
  50.     </LinearLayout>  
  51. </RelativeLayout>  
可見在列表項外面又包了一層RelativeLayout,將列表的根佈局LinearLayout靠父窗口右邊顯示即可。給RelativeLayout添加了半透明背景android:background="#66000000"
這樣要非常注意的是,根佈局RelativeLayout設置的android:layout_width和android:layout_height是無意義的,因爲我們會通過代碼重新設置:
  1. private void showPopupWindow() {  
  2.     View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  3.     mPopWindow = new PopupWindow(contentView);  
  4.     mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);  
  5.     mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);  
  6.     
  7.     ………………//設置各控件點擊響應  
  8.   
  9.     mPopWindow.showAsDropDown(mMenuTv);  
  10.   
  11. }  
在這裏,我們通過代碼將PopupWindow的width和height設置爲LayoutParams.FILL_PARENT。那大家可能會有疑問:爲什麼我在xml中佈局中明明寫好了根佈局RelativeLayout的寬和高,爲什麼非要我們在代碼中重新設置呢?不設置還顯示不出來…………
下篇,我們將會解答這個問題。
好啦,這個示例關鍵部分講完了,其它的大家就看源碼吧。
源碼在文章底部給出

五、爲PopupWindow添加動畫

先看看效果:


爲PopupWindow添加動畫並不難,只需要使用一個函數即可:

  1. //設置動畫所對應的style  
  2. mPopWindow.setAnimationStyle(R.style.contextMenuAnim);  
下面就來看看具體是如何添加動畫的

1、生成動畫對應的style

(1)、進入時的動畫:(context_menu_enter.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   
  4.     <translate  
  5.         android:duration="@android:integer/config_shortAnimTime"  
  6.         android:fromXDelta="0"  
  7.         android:fromYDelta="100%p"  
  8.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  9.         android:toXDelta="0"  
  10.         android:toYDelta="0"/>  
  11.   
  12. </set>  
這段代碼的意義就是從底部進入,即從Y軸100%的位置移動到0的位置。有關動畫的知識,大家可以參考《Animation 動畫詳解(一)——alpha、scale、translate、rotate、set的xml屬性及用法》      這個系列文章。

(2)、退出時動畫(context_menu_exit.xml)

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <translate  
  5.         android:duration="@android:integer/config_shortAnimTime"  
  6.         android:fromXDelta="0"  
  7.         android:fromYDelta="0"  
  8.         android:interpolator="@android:anim/accelerate_decelerate_interpolator"  
  9.         android:toXDelta="0"  
  10.         android:toYDelta="100%p" />  
  11.   
  12. </set>  
效果是從上向下移動。

(3)、最後,生成對應的style---contextMenuAnim

  1. <style name="contextMenuAnim" parent="@android:style/Animation.Activity">  
  2.     <item name="android:windowEnterAnimation">@anim/context_menu_enter</item>  
  3.     <item name="android:windowExitAnimation">@anim/context_menu_exit</item>  
  4. </style>  
android:windowEnterAnimation設置進場動畫,android:windowExitAnimation設置出場動畫。

2、使用AnimationStyle

使用時非常簡單,直接將對應的style通過setAnimationStyle設置進PopupWindow實例即可,代碼如下,難度不大,不再細講。
  1. private void showPopupWindow() {  
  2.     View contentView = LayoutInflater.from(MainActivity.this).inflate(R.layout.popuplayout, null);  
  3.     mPopWindow = new PopupWindow(contentView);  
  4.     mPopWindow.setWidth(ViewGroup.LayoutParams.FILL_PARENT);  
  5.     mPopWindow.setHeight(ViewGroup.LayoutParams.FILL_PARENT);  
  6.       
  7.     ………………//設置各子項點擊響應  
  8.     mPopWindow.setAnimationStyle(R.style.contextMenuAnim);  
  9.   
  10.     mPopWindow.showAsDropDown(mMenuTv);  
  11. }  
到這裏,這個示例的代碼就講完了。源碼在文章底部一起給出。這篇講述了有關PopupWindow的基本使用方法,下篇將針對還未講述的幾個函數,以及問題點結合源碼深入剖析。


源碼內容:

1、《PopshowAtLocation》:第二部分:簡單示例(showAtLocation顯示窗體)對應源碼

2、《PopupShowAsDropDown》:第三部分:另一示例(showAsDropDown顯示窗體) 對應源碼

3、《PopDropDownBg》:第四部分:提高:爲菜單添加陰影  對應源碼

4、《PopupAnim》:第五部分:爲PopupWindow添加動畫  對應源碼


如果本文有幫到你,記得加關注哦

源碼下載地址:http://download.csdn.net/detail/harvic880925/9195255

請大家尊重原創者版權,轉載請標明出處:http://blog.csdn.net/harvic880925/article/details/49272285   謝謝


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章