5

目前我有具有默認寬度溢出菜單:安卓:溢出菜單的變化幅度

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> 

但didn沒有成功。請任何人都可以幫忙。

回答

0

我follwing這個[http://keepsafe.github.io/2014/11/19/building-a-custom-overflow-menu.html]教程,但它沒有提到的方法來改變寬度

所以我也一直在尋找相同的答案,並一直在尋找的同時,所有的#2的問題是沒有答案。最後,我不得不挖掘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

對此==>

enter image description here