Implement pop up Menu with margin

China☆狼群 提交于 2019-11-28 10:54:17

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"

Lastly apply MyPopupMenu to your theme

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

This is a little late but I hope this helps someone.

I was trying to do what you were doing with a PopupMenu but nothing was working for me until I learned about ListPopupWindow. It's a way better alternative. Much more flexible in my opinion and you can achieve the margin-spacing you were asking about.

Here's the code:

public class MainActivity extends AppCompatActivity
{
    private ImageButton mMoreOptionsButton;
    private ArrayAdapter<String> mPopupAdapter;
    private ArrayList<String> mOptionsArray = 
            new ArrayList<>(Arrays.asList("Option1", "Option2", "Option3"));
    private ListPopupWindow mPopupWindow;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMoreOptionsButton = (ImageButton) view.findViewById(R.id.more_options_button);
        setupPopupWindow();
    }

    private void setupPopupWindow()
    {
        mPopupAdapter = new ArrayAdapter<>(MainActivity.this, android.R.layout.simple_spinner_dropdown_item, mOptionsArray);
        mPopupWindow = new ListPopupWindow(MainActivity.this);
        mPopupWindow.setAdapter(mPopupAdapter);
        mPopupWindow.setAnchorView(mMoreOptionsButton);
        mPopupWindow.setWidth(500);
        mPopupWindow.setHorizontalOffset(-380); //<--this provides the margin you need
        //if you need a custom background color for the popup window, use this line:
        mPopupWindow.setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(MainActivity.this, R.color.gray)));

        mPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                //do something with item by referring to it using the "position" parameter
            }
        });

        mMoreOptionsButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                mPopupWindow.show();
            }
        });
    }
}

The key part is calling mPopupWindow.setHorizontalOffset(). Be aware that this method is tricky. Depending on the value you set in mPopupWindow.setWidth() you will have to adjust the value in setHorizontalOffset() accordingly. It just so happened that for my app, -380 was the perfect amount of margin I needed from the end. So you may have to play with this value a bit.

I believe the same can be said for using setHeight() and setVerticalOffset() if you want some margin at the top of your popup window.

Hope this helps :]

You can set popUpWindow at particular location

popupWindow .showAtLocation(popupView, Gravity.CENTER, 0, 0);

public void showAtLocation(View parent, int gravity, int x, int y) {
        showAtLocation(parent.getWindowToken(), gravity, x, y);
    }

If you want to show it as DropDown then you can try

public void showAsDropDown(View anchor, int xoff, int yoff) {
        showAsDropDown(anchor, xoff, yoff, DEFAULT_ANCHORED_GRAVITY);
    }

Take a look on Documentation of PopupWindow http://developer.android.com/intl/es/reference/android/widget/PopupWindow.html

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!