Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

PopupWindow 學習實例:從版面配置到彈出顯示

編輯:Android 開發實例

PopupWindow 是一個浮動的彈出視窗,常用於實作「點一下按鈕跳出操作選單」這類互動。它比 Dialog 更輕巧,且不會搶走焦點,可以浮在任意元件旁邊或上方。以下用一個完整的例子走過整個流程。

步驟一:準備兩份 XML

main.xml —— 觸發用的按鈕

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

注意最外層 LinearLayout 給了 android:id="@+id/layout",這個 id 稍後會作為 PopupWindow 的定位參考點。

popup.xml —— 彈出視窗的內容

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

    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/fenxiang"
        android:id="@+id/tv_fenxiang"
        android:textColor="#000000"
        android:textSize="30sp" />
    <ImageView android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/imv_line" />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/jubao"
        android:id="@+id/tv_jubao"
        android:textColor="#000000"
        android:textSize="30sp" />
    <ImageView android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="@drawable/imv_line" />
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/cancel"
        android:id="@+id/tv_cancel"
        android:textColor="#000000"
        android:textSize="30sp" />
</LinearLayout>

這裡用「文字 + 一條 1dip 高的 ImageView 當分隔線」的做法組出分享、檢舉、取消三個選項。用 ImageView 當分隔線是常見技巧,因為它的高度與顏色都能完全自訂。

步驟二:Activity 中的程式碼

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button1 = (Button)findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v) {
            getPopupWindow();
            popup.showAtLocation(findViewById(R.id.layout),
                    Gravity.BOTTOM, 0, -70);
            // popup.showAsDropDown(findViewById(R.id.layout), 20, -220);
            // popup.showAsDropDown(findViewById(R.id.layout));
        }
    });
}

/** 創建 PopupWindow */
protected void initPopuptWindow() {
    // 獲取自定義布局文件 popup.xml 的視圖
    View popupWindow_view = getLayoutInflater().inflate(R.layout.popup, null, false);

    // 創建 PopupWindow 實例
    popup = new PopupWindow(popupWindow_view, 260, 160, true);

    tv_fenxiang = (TextView) popupWindow_view.findViewById(R.id.tv_fenxiang);
    tv_jubao = (TextView) popupWindow_view.findViewById(R.id.tv_jubao);
    tv_cancel = (TextView) popupWindow_view.findViewById(R.id.tv_cancel);

    popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.panel_bg));
    // menu 菜單獲得焦點,如果沒有獲得焦點,菜單中的控件事件無法響應
    popup.setFocusable(true);
    popup.update();
}

/** 獲取 PopupWindow 實例 */
private void getPopupWindow() {
    if(null != popup) {
        popup.dismiss();
        return;
    } else {
        initPopuptWindow();
    }
}

關鍵點逐一說明

PopupWindow 的四參數建構子

new PopupWindow(View contentView, int width, int height, boolean focusable)

最後那個 true 代表 focusable。這裡的 260, 160 是像素單位而非 dp,在不同解析度的裝置上顯示大小會不一致,實務上建議改用 WRAP_CONTENT 讓它依內容自動調整,或自行換算 dp 轉 px。

setFocusable(true) 為什麼必要

這行註解點出了關鍵:若 PopupWindow 沒有取得焦點,裡面的元件點擊事件完全不會響應。這是初學時最常遇到的「彈跳出來了但點不動」問題。不過要注意,focusable 設為 true 時,彈出視窗會攔截外部的觸碰事件,需要點擊外部区域時得自行實作 dismiss 邏輯。

setBackgroundDrawable 的必要性

這行不只是為了美觀。在部分 Android 版本上,若未設定背景,PopupWindow 將無法正常 dismiss(點擊外部關閉會失效)。所以即使只是設一個全透明的 placeholder 也建議設定。

兩種顯示方式

  • showAsDropDown(view, xoff, yoff):相對於指定 view 的下方顯示,偏移量可用負值往上調。這是最常用於「下拉選單」情境的做法。
  • showAtLocation(view, gravity, x, y):相對於整個視窗的指定方位顯示。範例用 Gravity.BOTTOM 搭配 y 偏移 -70,讓它貼在螢幕底部偏上一點。

雙擊收起的切換邏輯

getPopupWindow() 這段寫法實作了一個 toggle:若 popup 已存在就 dismiss 並返回,否則新建。這樣同一顆按鈕就能達到「點一下開、再點一下關」的效果。實務上更嚴謹的寫法是加上 popup.isShowing() 判斷,因為 popup 物件存在不代表它目前正在顯示。

三個實務提醒

  • 記得為三個 TextView 掛上 setOnClickListener,範例中只取得元件而未綁定事件,實際使用時要補上並在處理後呼叫 popup.dismiss()。
  • PopupWindow 也必須在 Activity 銷毀時確實 dismiss,否則會造成 window leak 警告。
  • 若只是想顯示一個簡單的操作選單,原生 PopupMenu 或 ListPopupWindow 會省事得多,它們已內建選項管理與關閉行為。自刻 PopupWindow 適合版面高度客製的情境。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved