Android UI開發第三篇——popupwindow

來源:互聯網
上載者:User

PopupWindow在android.widget包下,快顯視窗的形式展示。官方文檔對該控制項的描述是:“一個快顯視窗控制項,可以用來顯示任意視圖(View),而且會浮動在當前 活動(activity)的頂部”。PopupWindow可以讓我們實現多種自訂控制項,例如:menu、alertdialog等彈窗似的View。


      popupwindow需要在onCreate時初始化、通過觸發事件展示出來。
JAVA代碼:
public class ShowPopupWindow extends Activity implements View.OnClickListener {View view;PopupWindow pop;Button btnShowAsDrawDown;Button btnShowAsDrawDown1;Button btnShowAtLocation;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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();}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (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() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubpop.dismiss();}});}}

popup_activity.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:id="@+id/main"     android:layout_width="fill_parent"     android:layout_height="fill_parent">  <TextView        android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="pop demo!"     />  <Button        android:id="@+id/btnShowAsDrawDown"     android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="Show as drawndown(one parameter)"    />      <Button        android:id="@+id/btnShowAsDrawDown1"     android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="Show as drawndown(three parameters)"    />     <Button        android:id="@+id/btnShowAt"     android:layout_width="fill_parent"       android:layout_height="wrap_content"       android:text="Show At Location"    /></LinearLayout> 

popup_window.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical" android:gravity="center_horizontal"android:background="#d3d3d3"><Button android:id="@+id/btn_pop" android:layout_width="wrap_content"android:layout_height="wrap_content" android:text="Close"android:layout_margin="10dip" /></LinearLayout>
相關文章

聯繫我們

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