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

PopupWindow 學習實例

編輯:關於 Android 開發

PopupWindow 可在一個 view 之上浮現一個彈出視窗,常用於實作自訂的下拉選單或浮動操作面板。本文以一個包含分享、檢舉、取消三個選項的範例,示範 PopupWindow 的完整用法。

一、建立版面配置

首先在 main.xml 中建立一個觸發用的 Button:

<?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>

接著建立 popup.xml,放入三個 TextView 作為選單項目:

<?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>

二、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() {
    View popupWindow_view = getLayoutInflater().inflate(R.layout.popup, null, false);
    popup = new PopupWindow(popupWindow_view, 260, 160, true); // 建立 PopupWindow 實例
    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));
    popup.setFocusable(true); // 讓選單取得焦點,否則其中元件的事件無法回應
    popup.update();
}

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

重點在於:以 showAtLocation 或 showAsDropDown 決定彈出位置,並務必呼叫 setFocusable(true) 讓選單取得焦點,否則其中的元件將無法回應點擊事件。透過 getPopupWindow 的判斷,還能實現點擊時在顯示與關閉之間切換。

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