Change overflow menu border in Android

Here's the default position of the context menu in Android:

default position

However, the Inbox application has it farther from the top and right edges of the screen:

inbox

Is there a relatively direct way to achieve this?

+4
source share
6 answers

About Inbox

What you see in the screenshot does not quite match Menu, but FrameLayoutwith a background with shadows. This is just a custom view .

enter image description here
You can check it using the UI Viewer

So, you can do the same trick and instead of inflating, PopupMenucreate a custom one Viewand put it where you want.

, Menu

, Android Action ( 9 ).

menu_dropdown_panel.9:

enter image description here

paddings ( Paint.NET : , ):

enter image description here

android:popupBackground :

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <item name="popupMenuStyle">@style/PopupMenu.Example</item>
</style>

<style name="PopupMenu.Example" parent="@style/Widget.AppCompat.Light.PopupMenu">
    <item name="android:popupBackground">@drawable/menu_dropdown_panel_background</item>
</style>

:

enter image description here enter image description here
( , PopupMenu)

PopupMenu, :

PopupMenu popup = new PopupMenu(MainActivity.this, fab);
popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
popup.show();

,

+2

?

, . 5 .

, /. , . , , .

:

:

<item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item>

OverflowMenuStyle :

<style name="OverflowMenuStyle" parent="Widget.AppCompat.PopupMenu.Overflow">
    <item name="android:dropDownHorizontalOffset">-16dp</item>
    <item name="android:dropDownVerticalOffset">16dp</item>
</style>

:

:

enter image description here

:

enter image description here

(, , ):

:

/res/values/styles.xml:

<!-- Values defined here apply to all supported API versions unless overridden in children -->
<BaseTheme />

<!-- Defined values apply to all supported API versions unless overridden in res/values-vXX/styles.xml -->
<AppTheme extends BaseTheme />

/res/values-vXX/styles.xml:

<!-- Values defined here apply to all API versions >= XX unless overridden in res/values-vYY/styles.xml where YY > XX -->
<AppTheme extends BaseTheme />

actionOverflowMenuStyle BaseTheme. android: - actionOverflowMenuStyle, appcompat, Android.

+3

. .

- . , .

" " . , . showAsAction MenuItem. OnClick , ListPopupWindow MenuItem. ListPopupWindow , , ListPopupWindow.setHorizontalOffset() ListPopupWindow.setVerticalOffset()

+1

. , , Activity ( ActionBar), , , CardView . CardView , , , . .

0

, , , , , ,

 <style name="OverflowMenu" parent="Widget.AppCompat.PopupMenu.Overflow" tools:targetApi="21">
    <item name="overlapAnchor">false</item>
    <!--<item name="dropDownVerticalOffset">?attr/actionBarSize</item>-->
    <item name="android:overlapAnchor">false</item>
</style>

<item name="actionOverflowMenuStyle">@style/OverflowMenu</item>
0

I think this is exactly what you are looking for, you need to customize the view

public void onClick(View v) {
    // code from above
    // ...

    popupMenu.show();

    // Try to force some horizontal offset
    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 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);
    }
}

More info ... this

0
source

Source: https://habr.com/ru/post/1624383/


All Articles