PopupWindow还是PopupMenu?或者是其他东西?

时间:2014-07-23 14:29:00

标签: android popupwindow popupmenu

我正在尝试创建与Google Play图书非常相似的布局。我需要创建一个PopupMenu或PopupWindow,它将显示在相同的位置,如下图所示。

PopupWindow (or PopupMenu)

我不确定,如果是PopupMenuPopupWindow或其他内容。单击菜单中的项目时会显示此“窗口”。

如果我将方向更改为横向模式,则ActionBar中会显示一个新图标,显示此“窗口”。下图显示了这种情况:

PopupWindow (or PopupMenu) in landscape

我有两个问题:

  1. 这是PopupMenuPopupWindow还是其他什么?
  2. 我如何确定此“窗口”的位置,以防PopupWindow,我需要显示它?

1 个答案:

答案 0 :(得分:4)

你必须像这样创建一个OnClickListener

button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        int[] location = new int[2];
        v.getLocationOnScreen(location);

        // Initialize the Point with x, and y positions
        Point point = new Point();
        point.x = location[0];
        point.y = location[1];

        popupWindow(v, point);

    }

});

popupWindow(View view, Point point)的方法:

private ArrayAdapter<String> adapterTypeSelection;
private void popupWindow(View v, Point p) {

// int popupwindowWidth =
// UnitConverterClass.convertDpToPx(180,getActivity());
int popupwindowHeight = LinearLayout.LayoutParams.WRAP_CONTENT;

LayoutInflater layoutInflater = (LayoutInflater) getActivity()
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(
        R.layout.dashboard_profile_popup_window, null);

// Creating the PopupWindow
final PopupWindow pwindow = new PopupWindow(getActivity());
pwindow.setContentView(layout);
// pwindow.showAsDropDown(v);
// pwindow.setWidth(popupwindowWidth);
pwindow.setHeight(popupwindowHeight);
pwindow.setFocusable(true);

String[] types = {"hello", "hi"};

adapterTypeSelection = new ArrayAdapter<String>(getActivity(),
        R.layout.<your layout for the popupwindow>,
        R.id.textView, types);

ListView listview = (ListView) pwindow.getContentView().findViewById(
        R.id.listview_popwindow);

listview.setAdapter(adapterTypeSelection);

listview.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        TextView temp = (TextView) parent.getChildAt(position)
                .findViewById(R.id.textView);

        if (temp.getText()
                .toString()
                .equals("hello"))) {
            //hello
        } else {
            //hi
        }

        pwindow.dismiss();
    }
});

pwindow.setOnDismissListener(new OnDismissListener() {

    @Override
    public void onDismiss() {
        //TODO dismiss settings
    }

});

pwindow.setWidth(<width>);
pwindow.setBackgroundDrawable(<resource for background>);

// int OFFSET_X = UnitConverterClass.convertDpToPx(180, getActivity());
// int OFFSET_Y = UnitConverterClass.convertDpToPx(30, getActivity());
// pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, 
//                                p.x + OFFSET_X, p.y + OFFSET_Y);
pwindow.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);
}

希望这有助于让PopupWindow处于您想要显示它的位置。