編輯:Android 開發實例
PopupWindow 是一個浮動的彈出視窗,常用於實作「點一下按鈕跳出操作選單」這類互動。它比 Dialog 更輕巧,且不會搶走焦點,可以浮在任意元件旁邊或上方。以下用一個完整的例子走過整個流程。
<?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 的定位參考點。
<?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 當分隔線是常見技巧,因為它的高度與顏色都能完全自訂。
@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();
}
}
new PopupWindow(View contentView, int width, int height, boolean focusable)
最後那個 true 代表 focusable。這裡的 260, 160 是像素單位而非 dp,在不同解析度的裝置上顯示大小會不一致,實務上建議改用 WRAP_CONTENT 讓它依內容自動調整,或自行換算 dp 轉 px。
這行註解點出了關鍵:若 PopupWindow 沒有取得焦點,裡面的元件點擊事件完全不會響應。這是初學時最常遇到的「彈跳出來了但點不動」問題。不過要注意,focusable 設為 true 時,彈出視窗會攔截外部的觸碰事件,需要點擊外部区域時得自行實作 dismiss 邏輯。
這行不只是為了美觀。在部分 Android 版本上,若未設定背景,PopupWindow 將無法正常 dismiss(點擊外部關閉會失效)。所以即使只是設一個全透明的 placeholder 也建議設定。
Gravity.BOTTOM 搭配 y 偏移 -70,讓它貼在螢幕底部偏上一點。getPopupWindow() 這段寫法實作了一個 toggle:若 popup 已存在就 dismiss 並返回,否則新建。這樣同一顆按鈕就能達到「點一下開、再點一下關」的效果。實務上更嚴謹的寫法是加上 popup.isShowing() 判斷,因為 popup 物件存在不代表它目前正在顯示。
setOnClickListener,範例中只取得元件而未綁定事件,實際使用時要補上並在處理後呼叫 popup.dismiss()。PopupMenu 或 ListPopupWindow 會省事得多,它們已內建選項管理與關閉行為。自刻 PopupWindow 適合版面高度客製的情境。
Android 筆記:自製下拉刷新 ListView 的完整拆解
ListView 是 Android 裡應用最廣的元件之一,功能強大、擴充彈性高,前面幾篇我們陸續玩過分組、拖曳排序、3D 立體、游標、圓角等變形。這次要動手做的是另一個
Android 登入功能實作全解析:從 textPassword 到三次失敗鎖定
登入畫面是大多數 App 的第一道門。它看似只是「兩個輸入框加一顆按鈕」,但真正做過就會發現,密碼欄的遮蔽設定、按鈕的事件綁定、錯誤態度的回饋、以及防止暴力嘗試的機制,每
Android ViewPager 相簿橫向滑動實作
ViewPager 可用來做橫向相簿。需搭配 PagerAdapter,在 instantiateItem 建立頁面,在 destroyItem 回收。可定義 Image
Android 開發實戰:整合百度地圖 SDK 完整指南
百度地圖 Android SDK 讓開發者能夠輕鬆存取百度地圖服務與資料,打造功能豐富、互動性強的 LBS(位置服務)應用。它提供地圖顯示、定位、搜尋等一整套能力,且所有