Android:更改溢出菜单的宽度

时间:2013-07-17 07:15:15

标签: android android-actionbar android-theme android-styles android-optionsmenu

目前我有一个溢出菜单,默认宽度为:

enter image description here

我想要的是:

enter image description here

我尝试过这样改变主题:

<style name="MyWorkspaceDetailTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">

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

</style>

<style name="MyPopupMenu" parent="@android:style/Widget.Holo.ListPopupWindow">
    <item name="android:dropDownWidth">30dp</item>


</style>

但没有取得任何成功。请任何人都可以帮忙。

1 个答案:

答案 0 :(得分:0)

我正在阅读[http://keepsafe.github.io/2014/11/19/building-a-custom-overflow-menu.html]教程,但没有提到改变宽度的方法

所以我也在寻找相同的答案并一直在寻找,Stackoverflow上的所有问题都没有答案。最后,我不得不挖掘developer.google.com以找到方法。

http://developer.android.com/reference/android/widget/ListPopupWindow.html

你会找到一个实际完成我们工作的方法setContentWidth(int width)。

以下是答案

        //.......... Something on top
        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);
 /*********** THIS LINE DOSE OUR WORK and increases the width of OverFlow Menu ******/
            listPopupClass.getDeclaredMethod("setContentWidth", argTypes).invoke(listPopup, width+200);

            // 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("Soemthing", "Unable to force offset", e);
        }

enter image description here

到此==&gt;

enter image description here

相关问题