Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android界面篇[popupwindow]

Android界面篇[popupwindow]

編輯:關於Android編程

PopupWindow在android.widget包下,彈出窗口的形式展示。官方文檔對該控件的描述是:“一個彈出窗口控件,可以用來顯示任意視圖(View),而且會浮動在當前 活動(activity)的頂部”。PopupWindow可以讓我們實現多種自定義控件,例如:menu、alertdialog等彈窗似的View。
popupwindow需要在onCreate時初始化、通過觸發事件展示出來。
public class ShowPopupWindow extends Activity implements View.OnClickListener{
View view; PopupWindow pop; Button btnShowAsDrawDown; Button btnShowAsDrawDown1; Button btnShowAtLocation; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.popup_activity); btnShowAsDrawDown = (Button)findViewById(R.id.btnShowAsDrawDown); btnShowAsDrawDown.setOnClickListener(this); btnShowAsDrawDown1 = (Button)findViewById(R.id.btnShowAsDrawDown1); btnShowAsDrawDown1.setOnClickListener(this); btnShowAtLocation = (Button)findViewById(R.id.btnShowAt); btnShowAtLocation.setOnClickListener(this); initPopupWindow(); }
@Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { case R.id.btnShowAsDrawDown: if(pop.isShowing()) { pop.dismiss(); } else { pop.showAsDropDown(v); } break; case R.id.btnShowAsDrawDown1: if(pop.isShowing()) { pop.dismiss(); } else { pop.showAsDropDown(v, 0, -160); } break; default: if(pop.isShowing()) { pop.dismiss(); } else { pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 0, 0); } break; }

} private void initPopupWindow() { view = this.getLayoutInflater().inflate(R.layout.popup_window, null); pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); pop.setOutsideTouchable(true); view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub pop.dismiss(); } }); }
}
popup_activity.xml
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved