java Android Lollipop Material Design 溢出菜单图标颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26997231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:02:29  来源:igfitidea点击:

Android Lollipop Material Design Overflow Menu Icon color

javaandroidxmlandroid-5.0-lollipopmaterial-design

提问by code monkey

I try the new Material Design on a Nexus 7 and have the following strange behaviour. The Overflow Menu Icon has a different color on the first app launch.

我在 Nexus 7 上尝试了新的 Material Design 并有以下奇怪的行为。第一次启动应用程序时,溢出菜单图标具有不同的颜色。

I changed the android:textColorPrimarycolor and read this tutorial.

我改变了android:textColorPrimary颜色并阅读了本教程

  1. First App launch:first app launch

  2. Second App launch:enter image description here

  1. 第一个应用程序启动:第一个应用程序启动

  2. 第二个应用程序启动:在此处输入图片说明

As you see the color of the primary text color is not set on the first launch. It is only set if i press the home button and relaunch the app. Here is my styles.xmlfile:

如您所见,第一次启动时未设置主要文本颜色的颜色。只有当我按下主页按钮并重新启动应用程序时才会设置它。这是我的styles.xml文件:

<style name="AppBaseTheme" parent="android:Theme.Material.Light">
    <item name="android:colorPrimary">#FF4444</item>
    <item name="android:colorPrimaryDark">#CC0000</item>
    <item name="android:textColorPrimary">#000000</item>
</style>

Can someone explain, why that behaviour occurs?

有人可以解释一下,为什么会发生这种行为?

I set android:minSdkVersion="21"and don't want to use support libraries.

我设置android:minSdkVersion="21"并且不想使用支持库。

回答by Eugen Pechanec

I experienced the same problem when running a PreferenceActivitywhich cannot use appcompat-v7 library on a LOLLIPOPdevice. When this activity is opened for the first time the overflow icon is always white completely ignoring android:textColorPrimaryand android:colorControlNormal. Subsequent runs or orientation changes however result in proper coloring.

我在设备上运行PreferenceActivity无法使用 appcompat-v7 库时遇到了同样的问题LOLLIPOP。首次打开此活动时,溢出图标始终为白色,完全忽略android:textColorPrimaryandroid:colorControlNormal。然而,随后的运行或方向更改会导致正确的着色。

I created a gist which will help you mitigate this. The code binds a global layout observer to the toolbar and when it finds an overflow icon it replaces it and unbinds the observer. So don't use it in places where you don't expect an overflow icon because the observer won't get unbound in that case.

我创建了一个要点,可以帮助您缓解这种情况。代码将全局布局观察者绑定到工具栏,当它找到溢出图标时,它会替换它并解除观察者的绑定。所以不要在你不希望溢出图标的地方使用它,因为在这种情况下观察者不会被解除绑定。

Link to gist: https://gist.github.com/consp1racy/4b640679de553fdb3046

链接到要点:https: //gist.github.com/consp1racy/4b640679de553fdb3046

回答by Nihas Nizar

Just add android:theme="@style/ThemeOverlay.AppCompat.Dark"to the Toolbar

只需添加android:theme="@style/ThemeOverlay.AppCompat.Dark"到工具栏

<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_awesome_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

overflow menu icon will be white now :)

溢出菜单图标现在将是白色的 :)

回答by Denis Loh

Just add the secondary text color for the options menu, i.e.:

只需为选项菜单添加辅助文本颜色,即:

<item name="android:textColorSecondary">@color/text_color</item>

In some circumstances the secondary color is set to the primary color. I don't know yet why.

在某些情况下,次要颜色设置为主要颜色。我还不知道为什么。

回答by BzH

Add these items too:

也添加这些项目:

<item name="actionMenuTextColor">@color/white</item>
<item name="android:actionMenuTextColor">@color/white</item>

If this didn't help, then try this:

如果这没有帮助,请尝试以下操作:

<style name="AppBaseTheme" parent="android:Theme.Material.Light">
    <item name="android:itemTextAppearance">@style/TextAppearance</item>
</style>

<style name="TextAppearance">
    <item name="android:textColor">@android:color/white</item>
</style>

This would works for Holo.Light.DarkActionBar

这将适用于 Holo.Light.DarkActionBar

回答by jpardogo

If you want to use material design in pre-21 devices you need to extend Theme.AppCompat.Light.NoActionBartheme. To do that, you need to add compile com.android.support:appcompat-v7:21.0.0as a dependency of your project, this way you will be able to use the Toolbar in your layout.

如果你想在 pre-21 设备中使用材料设计,你需要扩展Theme.AppCompat.Light.NoActionBar主题。为此,您需要将 compile 添加com.android.support:appcompat-v7:21.0.0为项目的依赖项,这样您就可以在布局中使用工具栏。

Then define your theme in values/themes.xml::

然后在values/themes.xml 中定义你的主题:

<style name="Theme.MyTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/my_awesome_color</item>

    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/my_awesome_darker_color</item>

    <!-- colorAccent is used as the default value for colorControlActivated
     which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    <!-- You can also set colorControlNormal, colorControlActivated
    colorControlHighlight & colorSwitchThumbNormal. -->
</style>

and your the toolbar in your layout/my_activity.xml:

以及layout/my_activity.xml 中的工具栏:

<android.support.v7.widget.Toolbar
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/my_awesome_toolbar"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

You have optional attrs to define the theme and popupTheme as well:

您还有可选的属性来定义主题和 popupTheme:

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"

Chris Banes wrote a good article about this that you should read https://chris.banes.me/2014/10/17/appcompat-v21/

Chris Banes 写了一篇关于这个的好文章,你应该阅读https://chris.banes.me/2014/10/17/appcompat-v21/

I read on the comments of the questions that you are using eclipse, I highly recommend to use android studio + gradle and no eclipse.

我阅读了有关您使用 eclipse 的问题的评论,我强烈建议您使用 android studio + gradle 而没有 eclipse。