2014-03-28 17 views
1

我有一个溢出菜单的操作栏。 在该溢出菜单中,我在每个项目中都有文本。 当前项目对齐到左侧。 我希望它们能够通过XML对齐到溢出菜单的中心。溢出菜单项文本对齐到中心

下面是我的themes.xml - 在“myCustomMenuTextApearance”风格中,我使用了“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> 

任何帮助将不胜感激。

+0

您是否尝试设置<项目名= “机器人:宽度”> - 1DP上myCustomMenuTextApearance? – FMontano

+0

是的,但没有帮助。 –

+0

您是否找到解决方案? –

回答

0

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

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

MenuItem item = menu.getItem(positionOfMenuItem);

SpannableString s = new SpannableString(settingsItemTitle); (新的AlignmentSpan.Standard(Alignment.ALIGN_CENTER),0,s.length(),0);其中,

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