我在屏幕的右上角有一个弹出菜单。我想在右边加一些边距。怎么做?我无法找到任何解决方案。每次使用弹出窗口完成。但我想用弹出菜单。请帮忙。建议使用正确的代码,非常有帮助。
答案 0 :(得分:0)
你可以试试这个
<style name="PopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
<item name="android:dropDownHorizontalOffset">-4dp</item>
<item name="android:dropDownVerticalOffset">4dp</item>
</style>
在下面的代码中应用上面的样式
PopupMenu popup = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
popup = new PopupMenu(context, anchorView, Gravity.END, 0, R.style.MyPopupMenu);
}else{
popup = new PopupMenu(context, anchorView);
}
答案 1 :(得分:-1)
try {
Field fListPopup = menuHelper.getClass().getDeclaredField("mPopup");
fListPopup.setAccessible(true);
Object listPopup = fListPopup.get(menuHelper);
argTypes = new Class[] { int.class };
Class listPopupClass = listPopup.getClass();
// Get the width of the popup window
int width = (Integer) listPopupClass.getDeclaredMethod("getWidth").invoke(listPopup);
// Invoke setHorizontalOffset() with the negative width to move left by that distance
listPopupClass.getDeclaredMethod("setHorizontalOffset", argTypes).invoke(listPopup, -width);
// Invoke show() to update the window's position
listPopupClass.getDeclaredMethod("show").invoke(listPopup);
} catch (Exception e) {
// Again, an exception here indicates a programming error rather than an exceptional condition
// at runtime
Log.w(TAG, "Unable to force offset", e);
}