边际实现弹出菜单 [英] Implement pop up Menu with margin

查看:302
本文介绍了边际实现弹出菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用默认弹出菜单,并且期望它具有相同的行为.一切正常.我担心的是有关弹出菜单的呈现.我的弹出菜单位于屏幕的右侧.

I am using the default pop menu and expecting the behavior of the same. Everything is working fine. What my concern is regarding the rendering of pop up menu. My pop up menu sticks to the right of the screen.

我想要Android的Youtube应用使用的行为.

I want the behavior as used by Youtube app for android.

我主要无法为弹出菜单提供适当的边距.请帮忙. 我尝试过将重力提供给弹出菜单.但是弹出菜单会停留在屏幕右侧.

I am mainly not able to provide right margin to my pop up menu. Please help. I have tried providing Gravity to PopUp Menu. But Pop Up Menu sticks to the Right of screen.

PopupMenu popupMenu = new PopupMenu(mContext, anchor, Gravity.LEFT);
popupMenu.getMenuInflater().inflate(R.menu.menu_edit_accessory, popupMenu.getMenu());

推荐答案

您可以使用以下属性来更改PopupMenu的位置: 重力 dropDownHorizo​​ntalOffset dropDownVerticalOffset

You can change your PopupMenu's position by using the following attributes: gravity, dropDownHorizontalOffset and dropDownVerticalOffset

首先将重力设置为重力".

First set gravity to Gravity.END

popup.setGravity(Gravity.END);

然后通过创建样式来更改下拉偏移

Then change your dropdown-offsets by creating a style

<style name="MyPopupMenu" parent="@style/Widget.AppCompat.PopupMenu">
    <item name="android:dropDownHorizontalOffset">-4dp</item>
    <item name="android:dropDownVerticalOffset">4dp</item>
</style>

如果要与锚点视图重叠,请使用

If you want to overlap the anchor view use

parent="@style/Widget.AppCompat.PopupMenu.Overflow"

最后将MyPopupMenu应用于主题

Lastly apply MyPopupMenu to your theme

<item name="popupMenuStyle">@style/MyPopupMenu</item>

这篇关于边际实现弹出菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆