安卓:溢出菜单的变化幅度 [英] Android: change width of overflow menu

查看:208
本文介绍了安卓:溢出菜单的变化幅度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我已经有默认宽度溢出菜单:

Currently I have an overflow menu which has default width:

我要的是:

我试图改变的主题是这样的:

I have tried changing the theme this way:

<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>

但没有得到任何的成功。请谁能帮助。

but didn't got any success. Please can anyone help.

推荐答案

我follwing这个[的 http://keepsafe.github.io/2014/11/19/building-a-custom-overflow-menu.html] 教程,但它并没有提及方法来改变宽度

I was follwing this[http://keepsafe.github.io/2014/11/19/building-a-custom-overflow-menu.html] tutorial but it did not mention way to change width

所以我也希望同样的答案,并一直在寻找的同时,对所有的#1的问题是没有答案。最后,我不得不挖developer.google.com找出一种方式。

So i was also looking same answer and been searching for while, All the questions on Stackoverflow was unanswered. Finally i had to dig the developer.google.com to find out a way.

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

你会找到一个方法setContentWidth(int width)将它实际上做我们的工作。

you will find a method setContentWidth(int width) which actually does our work.

下面就是答案

        //.......... 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);
        }

输入图像的描述在这里

要这个==>

输入图像的描述在这里

这篇关于安卓:溢出菜单的变化幅度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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