ListPopupWindow 其實跟PopupWindow 顯示效果相同的,只不過他是一個寫好的ListView不需要重新寫佈局,只不過API跟PopupWindow 還是有點區別,本來想設置window顯示位置,但是並沒有找到 showAtLocation 類似的api。 但是找到了一些可以設置位置的api。記錄下來希望對大家有用。
ListPopupWindow listPopupWindow = new ListPopupWindow(NFCScanActivity.this);
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(NFCScanActivity.this,
android.R.layout.simple_list_item_1, lists);
//直接設置Adapter 就可以
listPopupWindow.setAdapter(stringArrayAdapter);
if (lists.size() > 10){
listPopupWindow.setHeight(ConvertUtils.dp2px(280));
}
listPopupWindow.setContentWidth(ConvertUtils.dp2px(180));
listPopupWindow.setHorizontalOffset(ConvertUtils.dp2px(80));
listPopupWindow.setVerticalOffset(-ConvertUtils.dp2px(80));
listPopupWindow.setAnchorView(fb_task);//設置ListPopupWindow的錨點,即關聯PopupWindow的顯示位置和這個錨點
listPopupWindow.setModal(true);//設置是否是模式
listPopupWindow.show();
- listPopupWindow.setContentWidth :設置內容的寬度
- listPopupWindow.setHorizontalOffset(ConvertUtils.dp2px(80)) : 水平方向的偏移量
- listPopupWindow.setVerticalOffset(-ConvertUtils.dp2px(80)) :豎直方向的偏移量
- listPopupWindow.setAnchorView(fb_task) : 設置錨點
- listPopupWindow.setDropDownGravity(Gravity.RIGHT) : 設置相對錨點的位置