【问题标题】:Changing menu item's text background in toolbar在工具栏中更改菜单项的文本背景
【发布时间】:2017-04-07 11:47:22
【问题描述】:

如何在单击菜单项时更改其文本背景? 整个项目的高亮颜色是正确的,但似乎文本有它自己的背景颜色。 我认为这笔交易是在 app:popupTheme 上设置的,但我不知道应该设置什么样式参数。

【问题讨论】:

    标签: android android-studio toolbar menuitem textcolor


    【解决方案1】:

    请使用它并设置所需的颜色。

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.your_menu, menu);
    
        int positionOfMenuItem = 0; // or whatever...
        MenuItem item = menu.getItem(positionOfMenuItem);
        SpannableString s = new SpannableString("My MenuItem");
        s.setSpan(new ForegroundColorSpan(Color.RED), 0, s.length(), 0);
        item.setTitle(s);
    }
    

    【讨论】:

      【解决方案2】:

      请查看How to change the background color of Action Bar's Option Menu in Android 4.2?

      更改文本颜色:设置 "android:textColor" 将起作用

      <style name="AppTheme.ActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
              ...
              <!--This would set the menu item color-->
              <item name="android:textColor">#000</item>
              ...
       </style>
      

      【讨论】:

      • 它不起作用。当整个项目的突出显示颜色为灰色时,文本颜色已更改,但文本背景的颜色 - 仍为白色(必须为灰色)。
      • 请浏览我在 answer@user3352926 中提供的链接
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多