我无法更改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>
答案 0 :(得分:1)
您可以准备自定义弹出菜单项布局文件,并使用您的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());
}
}