2014-03-28 8 views
1

오버플로 메뉴가있는 작업 표시 줄이 있습니다. 오버 플로우 메뉴에는 각 항목에 텍스트가 있습니다. 현재 항목이 왼쪽에 정렬됩니다. 그리고 XML을 통해 오버플로 메뉴의 가운데에 정렬하려고합니다.오버플로 메뉴 항목의 텍스트가 가운데 맞춤

아래 myes.xml입니다. "myCustomMenuTextApearance"스타일에서는 "gravity"속성을 사용했지만 작동하지 않았습니다. "gravity"속성을 "MyAppActionBarTheme"스타일로 옮겼을 때 항목이 중앙에 배치되었지만 TextView의 나머지 부분은 모두 내 Activity 레이아웃에있었습니다. 내가 원한 것이 아닌 것은.

나는 이러한 링크를 찾았지만 어떤 도움이 발견되지 않았다 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> 

도움이 될 것입니다.

당신은 동적 활동에 SpannableString를 사용하여 메뉴 항목을 중심으로 - 정렬 할 수 있습니다
+0

당신이 설정 시도 되세요 <항목 이름을 = "안드로이드 : 폭"> - myCustomMenuTextApearance에 1DP을? – FMontano

+0

예,하지만 도움이되지 않았습니다. –

+0

해결책을 찾았습니까? –

답변

0

:

`INT positionOfMenuItem = 0; // 또는 다른 위치

MenuItem item = menu.getItem (positionOfMenuItem);

SpannableStrings = new SpannableString (settingsItemTitle);

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

item.setTitle (들)`