ListView不在popupWindow之外

时间:2018-11-13 20:12:02

标签: android android-popupwindow

当我长按某些特定的画布坐标时,我想显示一个带有选项列表的简单菜单。 阅读一些帖子,我了解到,如果要在 event (x,y)坐标中显示该菜单,则必须使用 PopupWindow 。 现在,这是我的代码:

@Override
public void myLongPress(MotionEvent event) {
    ListView popupView = new ListView(anotherObject.getContext());

    String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
    ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(anotherObject.getContext(), android.R.layout.simple_list_item_1, stringArray);
    popupView.setAdapter(modeAdapter);

    int width = 500;
    int height = 100;
    boolean focusable = true;
    final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
    popupView.setBackgroundColor(Color.LTGRAY);
    /*
    popupView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            popupWindow.dismiss();
            return true;
        }
    });
    */

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        popupWindow.setElevation(20);
    }

    popupWindow.showAtLocation(anotherObject.getView() /*this is a random view*/, Gravity.NO_GRAVITY, (int)event.getX(), (int)event.getY());
}

菜单显示在正确的位置,但列表超出其范围(500x100)。我在尝试滚动列表时看到了这一点:

see my popupWindow

错误在哪里?您能建议我一个更好的解决方案,以便在特定点(未链接到视图)显示类似菜单吗?

0 个答案:

没有答案