Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 介面篇:PopupWindow 彈出視窗的完整用法

編輯:關於 Android 程式設計

PopupWindow 位於 android.widget 套件下,以彈出視窗的形式呈現內容。官方文件對它的描述是:「一個彈出視窗控制項,可以用來顯示任意的 View,並浮動在目前的 Activity 之上」。因為它能承載任何 View,所以自訂 menu、AlertDialog 風格的彈窗,都可以用它來實作。

使用流程很簡單:在 onCreate() 中初始化,之後透過觸發事件把它顯示出來。

Java 程式碼

public class ShowPopupWindow extends Activity implements View.OnClickListener {
    View view;
    PopupWindow pop;
    Button btnShowAsDrawDown;
    Button btnShowAsDrawDown1;
    Button btnShowAtLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        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) {
        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) {
                pop.dismiss();
            }
        });
    }
}

三個按鈕分別示範了兩種定位方式:showAsDropDown() 相對於觸發它的那個 View 顯示(可再帶 x、y 偏移量微調位置),showAtLocation() 則是相對於整個視窗以 Gravity 對齊。

版面配置:popup_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <TextView
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="pop demo!" />

    <Button
        android:id="@+id/btnShowAsDrawDown"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Show as drawndown(one parameter)" />

    <Button
        android:id="@+id/btnShowAsDrawDown1"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Show as drawndown(three parameters)" />

    <Button
        android:id="@+id/btnShowAt"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:text="Show At Location" />
</LinearLayout>

版面配置:popup_window.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:background="#d3d3d3"
    android:gravity="center_horizontal"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/btn_pop"
        android:layout_height="wrap_content"
        android:layout_margin="10dip"
        android:layout_width="wrap_content"
        android:text="Close" />
</LinearLayout>

幾個實務上值得留意的點:setOutsideTouchable(true) 讓使用者點擊彈窗外區域時可以互動,配合在 popup 內容的 View 上掛 setOnClickListener() 呼叫 dismiss(),就能做出「點任何地方關閉」的效果。另外記得用 isShowing() 判斷狀態,避免重複彈出。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved