Spinner 使用ListPopupWindow 显示下拉菜单,您可以使用它来显示多选项目选择列表:
private void showPopup() {
final ListPopupWindow lpw = new ListPopupWindow(this);
lpw.setAdapter(/*Your adapter here*/);
lpw.setAnchorView(mAnchor); // see below
lpw.setContentWidth(/*specific value*/); // see below
lpw.show();
// this is required because the popup's `ListView` will not be available
// until the ListPopupWindow is actually shown.
mAnchor.post(new Runnable() {
@Override
public void run() {
lpw.getListView().setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE);
}
});
}
然后您可以从MenuItem选择onOptionsItemSelected() 987654324 MenuItem。您还需要注意另外两件事:
mAnchor 是一个View,您需要将其插入到右上角的Activity 布局中,以便ListPopupWindow 将显示在正确的位置。例如,如果您有 Activity 根:
RelativeLayout 然后mAnchor 将是:
mAnchor = new View(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(0, 0);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
mAnchor.setLayoutParams(rlp);
// add mAnchorView first to the RelativeLayout
LinearLayout 然后mAnchor 将是:
mAnchor = new View(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(0, 0);
llp.gravity = Gravity.RIGHT;
mAnchor.setLayoutParams(llp);
// add mAnchorView first to the LinearLayout(assuming orientation vertical)
其他类型的布局以此类推。
其次,您需要将ListPopupWindow 的宽度设置为所需的值。您需要针对不同的屏幕尺寸和方向(如手机纵向和手机横向,纵向和横向的不同表格尺寸)调整此值。