gpt4 book ai didi

Android PopupMenu 将重力设置为 CENTER 不起作用

转载 作者:行者123 更新时间:2023-12-03 19:49:33 31 4
gpt4 key购买 nike

我试过这段代码。
并且还尝试了 CENTER_HORIZONTAL 和 CENTER_VERTICAL。
anchor 仍然在 View 的左侧

val menu = PopupMenu(this, view, Gravity.CENTER)
menu.inflate (R.menu.menu_avatar_2)
menu.show()

enter image description here

最佳答案

您无法使用默认 PopupMenu 控制它和PopupMenu(this, view, Gravity.CENTER)只设置PopuMenu的重力在您的 anchorView 的右侧.

如果你真的想在文本中有重力,这里有适合你的选项:

1:使用PopupWindow :

PopupWindow popupWindow = new PopupWindow(MainActivity.this);
popupWindow.setContentView(yourCustomView); // customview with list of textviews (with gravity inside)
popupWindow.showAsDropDown(anchorView); // display below the anchorview

2:使用 ListPopupWindow :
String[] products = {"Camera", "Laptop", "Watch", "Smartphone",
"Television", "Car", "Motor", "Shoes", "Clothes"};
ListPopupWindow listPopupWindow = new ListPopupWindow(MainActivity.this);
listPopupWindow.setAnchorView(view);
listPopupWindow.setDropDownGravity(Gravity.RIGHT);
listPopupWindow.setHeight(ListPopupWindow.WRAP_CONTENT);
listPopupWindow.setWidth(300);
listPopupWindow.setAdapter(new ArrayAdapter(MainActivity.this,
R.layout.list_item, products)); // list_item is your textView with gravity.
listPopupWindow.show();

关于Android PopupMenu 将重力设置为 CENTER 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47193572/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com