Android中快顯功能表PopupWindow的使用

來源:互聯網
上載者:User

Android中,使用手指觸摸,不能像電腦一樣有滑鼠左鍵和右鍵,所以會有一個PopupWindow來代替滑鼠右鍵。當使用者點擊的時候出現一個彈出的視窗與使用者交流資訊。好了,現在就開始介紹PopupWindow的用法。
首先聲明一個PopupWindow的對象
PopupWindow  pop=null;
初始化pop
pop=new PopupWindow(v,ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
第一個參數是要在PopupWindow中顯示的的View,第二個參數是PopupWindow的寬,我設定的是與其父視窗一樣寬,第三個參數是PopupWindow的高度,我設定的是包含它裡面的類容。
我們需要在PopupWindow中設定什麼樣的類容根據需要,我設定的是幾個Button,從xml檔案中解析出來的
LayoutInflater l=LayoutInflater.from(this);
 View v=l.inflate(R.layout.popup, null);
當使用者點擊按鈕的時候就讓PopupWindow顯示出來
@Override
public void onClick(View v) {
pop.showAsDropDown(button);

}
這時候PopupWindow就顯示在指定
showAsDropDown(View  anchor)
anchor的左下方。
當我們不需要PopupWindow的時候調用
pop.dismiss();

另外PopupWindow顯示的時候可以用showAtLocation()方法

void android.widget.PopupWindow.showAtLocation(View parent, int gravity, int x, int y)

parent a parent view to get the android.view.View.getWindowToken() token from
gravity the gravity which controls the placement of the popup window
x the popup's x location offset
y the popup's y location offset

第一個參數是要將PopupWindow放到的View,第二個參數是位置,第三第四是位移值

pop.showAtLocation(WorkPopUpTestActivity.this.ll, Gravity.BOTTOM, 0, 0);
如果是這樣的話,就將PopupWindow放到了View的左下角。

最後注意:只有當View載入完成之後才能顯示PopupWindow,如果View沒有載入完成就載入的話會不成功。
判斷View是否載入完成可以判斷其寬度是否為為其0,若否,則載入完成。然後我們再載入PopupWindow。這裡可以用Handler來實現。
[java]
1. <span style="font-size:18px;">@Override 
2.     public void run() { 
3.         // TODO Auto-generated method stub  
4.         boolean b=true; 
5.         while(b) 
6.         { 
7.             try { 
8.                 Thread.sleep(5); 
9.             } catch (InterruptedException e) { 
10.                 // TODO Auto-generated catch block  
11.                 e.printStackTrace(); 
12.             } 
13.             if(ll!=null) 
14.             { 
15.                 if(ll.getWidth()!=0) 
16.                 { 
17.                     hand.sendEmptyMessage(0); 
18.                     b=false; 
19.                 } 
20.             } 
21.              
22.              
23.         } 
24.          
25.     }</span> 
 如果載入完成就發送訊息。在Handler的handleMessage中處理訊息。代碼如下
[cpp]
1. Handler hand=new Handler() 
2.     { 
3.  
4.         @Override 
5.         public void handleMessage(Message msg) { 
6.             // TODO Auto-generated method stub  
7.             super.handleMessage(msg); 
8.             pop.showAtLocation(WorkPopUpTestActivity.this.ll, Gravity.BOTTOM, 0, 0); 
9.              
10.         } 
11.          
12.     };       www.2cto.com
 在handleMessage中顯示PopupWindow。


 

作者:zhy_cheng

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.