我正在尝试像ListPopUpWindow
这样的twitter
类似菜单Popup
。但后来它停留在屏幕的右侧。任何帮助将不胜感激。我的代码在下面
PopUpWindow方法
private void onListPopUp(View anchor) {
ArrayList<Person> persoonItem = new ArrayList<>();
persoonItem.add(new Person(R.drawable.ic_profile_logo, "Oladeji Abubakar", "abubakar.oladeji@deliveryscience.co", ""));
persoonItem.add(new Person(0, "", "", "Update Phone"));
persoonItem.add(new Person(0, "", "", "Your Cards"));
persoonItem.add(new Person(0, "", "", "Invite Friends"));
persoonItem.add(new Person(0, "", "", "Logout"));
ListPopupWindowAdapter mListPopUpWindowAdapter = new ListPopupWindowAdapter(getApplicationContext(), persoonItem);
final ListPopupWindow pop = new ListPopupWindow(this);
pop.setAdapter(mListPopUpWindowAdapter);
pop.setAnchorView(anchor);
pop.setModal(true);
pop.setContentWidth(toolbar.getWidth() / 2);
pop.setHeight(ListPopupWindow.WRAP_CONTENT);
pop.setVerticalOffset(-105);
//pop.setHorizontalOffset(100);
//pop.setPromptPosition(20);
//pop.setHorizontalOffset();
pop.setPromptPosition(0);
pop.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
pop.dismiss();
Toast.makeText(TestActivity.this, "Clicked" + ((Person) parent.getItemAtPosition(position)).getOthers(), Toast.LENGTH_LONG).show();
}
});
pop.show();
WindowManager.LayoutParams wlp = new WindowManager.LayoutParams(toolbar.getWidth()/2, LinearLayout.LayoutParams.WRAP_CONTENT);
wlp.gravity = Gravity.TOP | Gravity.END;
wlp.horizontalMargin = -20;
wlp.x = -20;
View parentView = (View) pop.getListView().getParent();
parentView.setLayoutParams(wlp);
}
图片
这就是我想要实现的目标
答案 0 :(得分:1)
您可以使用ListPopupWindow#setHorizontalOffset和ListPopupWindow#setVerticalOffset来偏移ListPopupWindow。
令人困惑的是,如果锚点位于屏幕的右侧,则需要将ListPopupWindow的宽度值添加到水平偏移量,而垂直偏移量始终基于0。将ListPopupWindow锚定到右侧时,使用宽度以下的值作为水平偏移量似乎没有任何效果。另外,水平轴上的正数也指向右侧,因此我们将水平值取反。
以下代码将ListPopupWindow 5dp向下定位,并将5dp从位于右上角的锚点向左定位:
final ListPopupWindow popupWindow = new ListPopupWindow(context);
int widthPixels = convertDpToPixels(200, context);
int horizontalOffset = -(widthPixels + convertDpToPixels(5, context));
int verticaloffset = convertDpToPixels(5, context);
popupWindow.setAnchorView(anchor);
popupWindow.setWidth(widthPixels);
popupWindow.setHorizontalOffset(horizontalOffset);
popupWindow.setVerticalOffset(verticaloffset);