編輯:關於 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>// 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 好用的地方:位置、樣式、動畫全部由自己決定。
(目前還沒找到滿意的動畫效果,所以暫時沒加;如果各位有推薦的,還請告知一二,在此謝過。)
效果圖:

Cortex-M0 地址對齊踩坑記:微信硬體平台 Demo 二次開發中的 HardFault
先看 Demo 的數據格式 微信硬體平台提供的 Demo 在傳輸數據時,會先把有效負載打包,再前後各加一段固定包頭與包尾後發出。 官方 Demo 原本只是為了實現「點燈
Android 自訂 AlertDialog 與 Activity 雙向傳值實作
Android 自訂 AlertDialog 與 Activity 雙向傳值實作 本文要達成的目標: 1、把 Activity 中 TextView 的字串取出來,設定到
自訂環形(圓形)進度條的實作教學
本文參考並借鑑自 https://blog.csdn.net/jdsjlzx/article/details/42497135 先看效果圖: 自訂控制項:Attenda
Android adb shell 指令大全:從模擬器管理到檔案傳輸
Android adb shell 指令大全:從模擬器管理到檔案傳輸 adb(Android Debug Bridge)是 Android 開發與除錯不可或缺的利器。本