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

用 PopupWindow 做下拉選單:比 Spinner 更好客製的實作

編輯:關於 Android 開發

   最近在做下拉選單。原本想用 Spinner,無奈它達不到專案的要求;跟同學同事問了一圈,發現大家都在用 PopupWindow。

   查了一下,PopupWindow 確實簡單,可客製性又強,正好符合需求。於是參考網上的寫法,自己從頭撸了一遍,順手寫成筆記免得日後忘記。

第一步:寫自訂佈局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="110dp" android:layout_height="wrap_content">
    <LinearLayout android:layout_width="100dp" android:layout_height="wrap_content"
        android:background="@drawable/bg_circle_drop_down_qr_code"
        android:orientation="vertical"
        android:layout_marginRight="@dimen/padding_10"
        android:paddingLeft="@dimen/padding_5" android:paddingRight="@dimen/padding_5"
        android:paddingTop="@dimen/padding_5">

        <LinearLayout android:id="@+id/lin_scan_qr_code"
            android:layout_width="match_parent" android:layout_height="0dp"
            android:layout_weight="1" android:gravity="center" android:orientation="horizontal"
            android:paddingBottom="@dimen/padding_5" android:paddingTop="@dimen/padding_5">
            <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/ic_circle_scan_qr_code" />
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/padding_10" android:gravity="center"
                android:text="掃一掃" android:textColor="@color/color_white"
                android:textSize="@dimen/text_16" />
        </LinearLayout>

        <View android:layout_width="wrap_content" android:layout_height="1px"
            android:layout_marginLeft="@dimen/padding_3" android:layout_marginRight="@dimen/padding_3"
            android:background="@color/color_white" />

        <LinearLayout android:id="@+id/lin_my_qr_code"
            android:layout_width="match_parent" android:layout_height="0dp"
            android:layout_weight="1" android:gravity="center" android:orientation="horizontal"
            android:paddingBottom="@dimen/padding_5" android:paddingTop="@dimen/padding_5">
            <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:src="@drawable/ic_circle_my_qr_code" />
            <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/padding_10" android:gravity="center"
                android:text="二維碼" android:textColor="@color/color_white"
                android:textSize="@dimen/text_16" />
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

第二步:建立 PopupWindow 並綁定點擊

// 1. 取得自訂佈局
View customView = getActivity().getLayoutInflater()
        .inflate(R.layout.lay_circle_pop_drop_down_qr_code, null, false);

// 2. 建立 PopupWindow 實例,寬 110dp(dp 轉 px),高隨內容
mQrCodePopWindow = new PopupWindow(customView,
        CommonUtil.dipToPx(getContext(), 110),
        ViewGroup.LayoutParams.WRAP_CONTENT, true);

// 3. 動畫效果(R.style.AnimationFade 為自行定義)
mQrCodePopWindow.setAnimationStyle(R.style.AnimationFade);
mQrCodePopWindow.setTouchable(true);
mQrCodePopWindow.setOutsideTouchable(true);
mQrCodePopWindow.setBackgroundDrawable(new BitmapDrawable());

customView.findViewById(R.id.lin_scan_qr_code).setOnClickListener(v -> {
    ToastUtil.show(getContext(), "掃一掃");
    dismissQrCodePopWindow();
});
customView.findViewById(R.id.lin_my_qr_code).setOnClickListener(v ->
        ToastUtil.show(getContext(), "二維碼"));

這裡有個實測確認的細節:建構子最後那個 true 是 setFocusable。若希望能「點空白處收起 PopupWindow」,setFocusable(true) 與 setBackgroundDrawable() 兩者必須同時具備,缺一就收不起來——只設 focusable 而不給背景,外面那一層會吃不到點擊事件。

第三步:控制顯示與隱藏

@OnClick(R.id.lin_top_right)
public void onClick(View v) {
    if (mQrCodePopWindow != null && mQrCodePopWindow.isShowing()) {
        mQrCodePopWindow.dismiss();
    } else {
        initQrCodePopWindow();
        mQrCodePopWindow.showAsDropDown(v);
    }
}

用 isShowing() 做切換判斷,再搭配 showAsDropDown(錨定 view),就能讓選單準確貼齊按鈕下方——這也是它比 Spinner 好用的地方:位置、樣式、動畫全部由自己決定。

(目前還沒找到滿意的動畫效果,所以暫時沒加;如果各位有推薦的,還請告知一二,在此謝過。)

效果圖:

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