編輯:關於 Android 開發
PopupWindow 是 Android 中常用的彈出式容器,可以在當前介面之上浮現一段自定義內容。本文透過完整範例,示範如何建立 PopupWindow、設定背景與動畫,並處理其中的按鈕點擊事件。
先來看幾張效果示意圖:





package com.example.popupwindown; import android.os.Bundle; import android.app.Activity; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showWindow(View v){ View view=getLayoutInflater().inflate(R.layout.pop, null); Button button = (Button) view.findViewById(R.id.operate); final TextView textview=(TextView) view.findViewById(R.id.textView1); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { /*Toast.makeText(MainActivity.this, "button is pressed", Toast.LENGTH_SHORT).show();*/ textview.setText("變了"); } }); PopupWindow pop=new PopupWindow(view, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); pop.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.btn_default)); pop.setAnimationStyle(android.R.style.Animation_Translucent); pop.getBackground().setAlpha(100); pop.setTouchable(true);//如果需要按鈕單擊事件,必須有 pop.setOutsideTouchable(true); pop.setFocusable(true); pop.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE); pop.showAtLocation(v,Gravity.CENTER, 0,0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }主介面只需要一個觸發用的按鈕,透過 android:onClick 綁定 showWindow 方法。
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="43dp" android:layout_marginTop="23dp" android:text="彈出窗口" android:onClick="showWindow" /> </RelativeLayout>
這個布局檔用來定義彈出窗口內實際顯示的內容。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> <Button android:id="@+id/operate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="操作" /> </LinearLayout>
執行結果如下:

Android Studio 專案導入與設定修改指南
Android Studio 專案導入與設定修改指南 從既有專案或範例程式進入 Android Studio 時,常需要調整 Gradle、SDK、模組結構與執行設定。以
自訂 Dialog 中的 EditText 無法彈出軟鍵盤:原因與解法
最近獨立開發的專案《全醫會》已在內測階段,很快會上架各大應用市場。之前幾個專案因為種種原因無緣上架,始終有些遺憾,所以這次心情格外好。 今天在做一個新專案,是專為律師與客
Android 原始碼設計模式:中介者模式解析與實踐
Android 原始碼設計模式:中介者模式解析與實踐 中介者模式又稱調解者模式或調停者模式,屬於行為型模式。它用一個中介物件封裝一系列物件之間的互動,讓各物件不必直接依賴
Android 自定義控件:obtainStyledAttributes 系列函數詳解
Android 自定義控件:obtainStyledAttributes 系列函數詳解 在開發自定義控件時,經常需要讀取 XML 中設定的屬性。這時通常會用到 obtai