我无法更改Appcompat PopupMenu的宽度,因为菜单项在
MenuPopupHelper.java中使用此布局abc_popup_menu_item_layout.xml,它将菜单项minWidth设置为196dip.
<android.support.v7.internal.view.menu.ListMenuItemView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="?attr/dropdownListPreferredItemHeight" android:minWidth="196dip" android:paddingRight="16dip">
我试图在style.xml的以下属性中覆盖minWidth和dropDownWidth.我也尝试在android:dropDownItemStyle中覆盖minWidth,但它没有帮助.我开始怀疑是否可以覆盖ListMenuItemView的这个硬编码属性.有没有人成功修改PopupMenu宽度?
<style name="OrderDetailsActivity" parent="AppTheme"> <item name="android:textAppearanceLargePopupMenu">@style/OrderDetailsPopupMenuTextAppearanceLarge</item> <item name="android:textAppearanceSmallPopupMenu">@style/OrderDetailsPopupMenuTextAppearanceSmall</item> <item name="android:dropdownlistviewStyle">@style/OrderDetailsListDropDownStyle</item> <item name="dropdownlistviewStyle">@style/OrderDetailsListDropDownStyle</item> </style> <style name="OrderDetailsPopupMenuTextAppearanceLarge" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Large"> <item name="android:fontFamily">sans-serif-medium</item> <item name="android:textSize">@dimen/font_size_medium</item> </style> <style name="OrderDetailsPopupMenuTextAppearanceSmall" parent="@android:style/TextAppearance.DeviceDefault.Widget.PopupMenu.Small"> <item name="android:fontFamily">sans-serif-medium</item> <item name="android:textSize">@dimen/font_size_medium</item> </style> <style name="OrderDetailsListDropDownStyle" parent="Widget.AppCompat.ListView.DropDown"> <item name="android:divider">@drawable/list_divider</item> </style>
解决方法
您可以准备自定义弹出菜单项布局文件,并使用Application类的onCreate()方法中的反射更新此静态字段:
private void updatePopUpMenuItemLayout() { try { Field field = MenuPopupHelper.class.getDeclaredField("ITEM_LAYOUT"); field.setAccessible(true); field.set(null,R.layout.custom_popup_menu_item_layout); } catch (Exception e) { Log.e("TAG",e.getMessage()); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。