溢出菜单项文本与中心对齐

时间:2014-03-28 15:35:09

标签: android android-actionbar overflow-menu

我有一个带溢出菜单的操作栏。 在那个溢出菜单中,我在每个项目中都有文字。 目前这些项目是左对齐的。 我希望它们通过XML在溢出菜单中居中对齐。

下面是我的themes.xml - 在“myCustomMenuTextApearance”样式中,我使用了“gravity”属性,但它不起作用。 当我将“gravity”属性移动到“MyAppActionBarTheme”样式时,它的项目是居中的,但是我的Activity布局中还有TextView的所有其余部分。 这根本不是我想要的。

我找到了这些链接,但没有找到任何帮助: 1. https://stackoverflow.com/questions/22702588/align-items-inside-of-a-menu-coming-from-actionbar

How to align menu item text in Actionbar overflow menu

Align Center Menu Item text in android

<?xml version="1.0" encoding="utf-8"?>

<style name="MyAppActionBarTheme" parent="android:Theme.Holo.Light">
    <item name="android:popupMenuStyle" tools:targetApi="11">@style/MyApp.PopupMenu</item>
    <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>
    <item name="android:actionBarStyle" tools:targetApi="11">@style/MyApp.ActionBar</item>

</style>

<!-- The beef: background color for Action Bar overflow menu -->
<style name="MyApp.PopupMenu" parent="android:Widget.Holo.Light.ListPopupWindow">
    <item name="android:popupBackground">@drawable/ab_menu_bkg</item>

</style>

<style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.TextView.PopupMenu">
    <item name="android:textColor">@color/red</item>
    <item name="android:gravity">center_horizontal</item>
</style>

<!-- Bonus: if you want to style whole Action Bar, not just the menu -->
<style name="MyApp.ActionBar" parent="android:Widget.Holo.Light.ActionBar.Solid">

    <!-- Blue background color & black bottom border -->
    <item name="android:background">@color/red</item>
</style>

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

您可以使用活动中的SpannableString动态居中对齐菜单项:

`int positionOfMenuItem = 0; //或任何其他位置

MenuItem item = menu.getItem(positionOfMenuItem);

SpannableString s = new SpannableString(settingsItemTitle);

s.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_CENTER),0,s.length(),0);

item.setTitle(一个或多个);`

相关问题