簡體   English   中英

如何在Theme.AppCompat.Light.DarkActionBar主題中更改菜單項的文本顏色?

[英]How to change the text color of a menu item in Theme.AppCompat.Light.DarkActionBar theme?

我有黑色背景,白色字母。

有任何想法嗎? 謝謝。

        <style name="PacerTheme"            
                parent="@style/Theme.AppCompat.Light.DarkActionBar">                                
            <item name="android:itemBackground">@android:color/black</item>
            <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>                                           
        </style>

        <style name="myCustomMenuTextApearance" parent="@style/Widget.AppCompat.Light.ActionBar.Solid">
            <item name="android:textColor">@android:color/white</item>             
        </style>

正常:

正常

單擊菜單按鈕,然后顯示:

菜單

修改樣式如下:

<style name="PacerTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="android:itemTextAppearance">@style/myCustomMenuTextApearance</item>                                           
</style>

<style name="myCustomMenuTextApearance" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
    <item name="android:textColor">@android:color/white</item>
</style>

對於“ myCustomMenuTextApearance”樣式,請為父項使用

@android:style/TextAppearance.Widget.IconMenu.Item

代替

@style/Widget.AppCompat.Light.ActionBar.Solid

我找到了解決方法,程序更改了文本顏色,如下所示:

  @Override
  public boolean onPrepareOptionsMenu(Menu menu) 
  {
    for(int i=0; i<menu.size(); i++) 
    {
        MenuItem mi = menu.getItem(i);
        String title = mi.getTitle().toString();
        Spannable newTitle = new SpannableString(title);        
        newTitle.setSpan(new ForegroundColorSpan(Color.WHITE), 0, newTitle.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mi.setTitle(newTitle);
    }
    return true;
  }

XML如下:

 <style name="PacerTheme"            
        parent="@style/Theme.AppCompat.Light.DarkActionBar">                                
        <item name="android:itemBackground">@android:color/black</item>                                         
 </style>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM