android 三種彈出框之一poprpWindow

來源:互聯網
上載者:User

標籤:android   des   style   blog   class   code   

poprpWindow

  在android的彈出框我目前瞭解到的是有三種:AlertDialog,poprpWindow,Activity偽彈框,

  AlertDialog太熟悉了,這裡就不介紹了

  就先看看poprpWindow

  API 給出的解釋是:

  

  意思就是一個展示view的彈出表單,這個彈出表單將會浮動在當前activity的最上層,

  它和AlertDialog的區別是:在android中彈出框有兩種方式:AlertDialog和PopupWindow,它們的不同點在於:
      1、AlertDialog的位置固定,而PopupWindow的位置可以隨意;
      2、AlertDialog是非阻塞線程的,而PopupWindow是阻塞線程的

  下面就通過一個例子來說明PopupWindow的用法:

  Activity.class:

 1 public class MainActivity extends Activity { 2  3     private Button btn; 4  5     @Override 6     protected void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.activity_main); 9         btn = (Button)findViewById(R.id.show);10         btn.setOnClickListener(new Button.OnClickListener() {11             12             @Override13             public void onClick(View v) {14                 PopupWindow popupWindow = new PopupWindow(MainActivity.this);15                 //這裡是用在popupWindow中顯示一個ListView, 你也可以載入一個xx.xml檔案(Layoutfalter.from().inflate())16                 ListView listView = new ListView(MainActivity.this);  17                 ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_dropdown_item_1line, new String[]{"第一個" , "第二個"});18                 listView.setAdapter(adapter);19                 popupWindow.setContentView(listView);   //PopupWindow中要顯示的view20                 popupWindow.setHeight(500);21                 popupWindow.setWidth(180);22                 popupWindow.setOutsideTouchable(true);  //在PopupWindow範圍外 預設為false23                  popupWindow.setBackgroundDrawable(new ColorDrawable(Color.argb(50, 52, 53, 55)));24                 /**25                  * popupWindow.showAsDropDown(anchor, xoff, yoff)    用於標記Popupwindow要顯示的在什麼位置26                  * anchor :至以那個view為標準位置27                  * xoff :在 anchor為基準位置的x位移量28                  * yoff :在 anchor為基準位置的y位移量29                  */30                 popupWindow.showAsDropDown(btn, 0, 0);31                 32             }33         });34         35     }36 37     @Override38     public boolean onCreateOptionsMenu(Menu menu) {39         getMenuInflater().inflate(R.menu.main, menu);40         return true;41     }42 43 }

對應的xml檔案就包含一個button就不貼出來了

源碼下載:源碼

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.