【问题标题】:action item in menu set text color菜单设置文本颜色中的操作项
【发布时间】:2016-10-31 22:46:58
【问题描述】:

我有一个在 3 种不同活动中膨胀到工具栏上的菜单。其中一项活动有一个溢出菜单,溢出后面有一个操作。 我可以更改动作的背景颜色。我无法更改与动作关联的文本的文本颜色。请不要投反对票,我已经查看并尝试了所有 136 个类似的发布问题。 下面是样式代码和菜单代码。

<resources>
<!-- Base application theme. -->
<!-- parent="Theme.AppCompat.NoActionBar"> -->
<!-- Line of CODE ABOVE gives WHITE color to items in ToolBar -->
<!-- Line of CODE BELOW gives BLACk color to items in ToolBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:itemBackground">@color/color_Red</item>
    <item name="actionMenuTextColor">@style/MyActionBarTitleText</item>
</style>

<style name="MyActionBarTitleText" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
    <item name="android:textColor">@color/color_Yellow</item>
</style>

菜单代码

<menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  tools:context=".MainActivity">

<item
    android:id="@+id/action_ReSetPW"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"
    android:visible="true"/>

作为旁注,我也无法根据某些参数更改标题文本颜色。我想知道如何设置或更改文本颜色? 经过一些试验和错误后,我发布了一个带有 cmets 的样式代码,用于下面的解决方案代码

<resources>
<!-- Base application theme. -->
<!-- parent="Theme.AppCompat.NoActionBar"> -->
<!-- Line of CODE ABOVE gives WHITE color to items in ToolBar -->
<!-- Line of CODE BELOW gives BLACk color to items in ToolBar -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="android:itemBackground">@color/color_Red</item>
    <!--The line of CODE above styles the action background color  -->

</style>

<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textSize">21sp</item>
    <item name="android:textStyle">italic</item>
</style>

<!--ADD THIS TO THE XML that defines the widget ToolBar android:theme="@style/ToolbarTheme" -->
<!--the line of CODE above tells the ToolBar where to find the style to use -->
<!--Code below will make the Overflow three dots Yellow and the text of the Action Yellow -->
<style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="android:textColorPrimary">@color/color_Yellow</item>
</style>

似乎解决方案的一部分是从toolbar.xml调用样式

【问题讨论】:

    标签: android menu android-actionbar


    【解决方案1】:

    编辑:

    This 答案有效:

    styles.xml

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:itemTextAppearance">@style/itemTextStyle.AppTheme</item>
    </style>
    
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
    
    <style name="itemTextStyle.AppTheme" parent="@android:style/TextAppearance.Widget.IconMenu.Item">
        <item name="android:textColor">@color/color_item_popup</item>
    </style>
    

    res/color/color_item_popup.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="#FFFF00"/>
        <item android:state_focused="true" android:color="#FFFF00"/>
        <item android:color="#00FFFF"/>
    </selector>
    

    【讨论】:

    • 我试过没有运气!让我感到困惑的是,背景颜色是直接在基本样式中设置的,还有其他想法吗?
    • 试试这个答案:stackoverflow.com/questions/24652964/… 似乎是合法的!
    • 然后用你喜欢的颜色替换@color/color_item_popup,你就不需要额外的xml文件了。
    • 我使用了您的部分代码,它非常广泛,我应该在第一次看到它时仔细查看它。看我的编辑它有效,但我还没有弄清楚它的所有来龙去脉。我缺少的是 ToolBar.XML 中的一行,谢谢
    【解决方案2】:

    你们俩都有正确的想法,我可能建议您查看这个 ThemeOverlay 链接,它解释了 ThemeOverlay vrs 主题。值得你花时间 James_Duh

    ThemeOverlay

    怎么会有人知道 TEXT textColorPrimary 所引用的内容。 我猜这都在 NAME 中

    <item name="android:textColorPrimary">@color/color_White</item>
    

    【讨论】:

    • 链接解释了很多,尤其是我使用这行代码的命名,现在只有 TEXT 设置为我想要的颜色,溢出与其他活动保持一致
    【解决方案3】:
    app:itemTextColor="@color/your_color"
    
    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view_search"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/drawer_menu"
        app:itemTextColor="@color/colorWhite"
        app:itemIconTint="@android:color/white"
        android:background="@drawable/nav_drawer_background" />
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关 如何 和/或 为什么 解决问题的附加 context 将改善答案的长度长期价值。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。提及为什么这个答案比其他答案更合适也没有什么坏处。
    猜你喜欢
    • 2013-08-03
    • 2014-09-11
    • 1970-01-01
    • 2015-03-31
    • 2012-05-31
    • 2015-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-05-30
    相关资源
    最近更新 更多