標籤: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就不貼出來了
源碼下載:源碼