【问题标题】:Material Design theme in Android app - menu item icon isn't inheriting from the themeAndroid 应用中的 Material Design 主题 - 菜单项图标未从主题继承
【发布时间】:2019-12-12 13:33:54
【问题描述】:

我是 Android 开发的新手,如果这是一个愚蠢的问题,但 Google 和过去的 SO 问题没有帮助,我很抱歉。

我想为我的应用使用深色主题,所以我将主题设置为:

<style name="AppTheme" parent="Theme.MaterialComponents">

我有一个工具栏,其中包含一个包含 2 个项目的菜单。菜单定义为:

<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="com.mycompany.myapp.MainActivity">

    <item
        android:id="@+id/action_contacts"
        android:icon="@drawable/ic_contacts_24"
        android:title="@string/action_contacts"
        app:showAsAction="always" />

    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        app:showAsAction="never" />
</menu>

我遇到的问题是,虽然工具栏正确使用了深色主题,但第一个菜单项 (@+id/action_open_contacts) 将图标颜色设置为黑色,因此几乎看不到它。溢出菜单工作正常,并显示一个白色菜单图标。

为什么联系人图标不显示为白色?这不应该像菜单图标一样从应用程序的主题继承吗?

【问题讨论】:

    标签: android xml android-studio material-design android-theme


    【解决方案1】:

    你可以用 .

    包裹它

    将此文件添加到可绘制文件夹 - settings_icon.xml

    <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
        android:src="@drawable/ic_contacts_24"
        android:tint="@color/colorBlack"/>
    

    创建 colorBlack 或输入十六进制颜色,如#000000。

    然后将您的可绘制对象用于菜单项:

    <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="com.mycompany.myapp.MainActivity">
    
        <item
            android:id="@+id/action_contacts"
            android:icon="@drawable/settings_icon"
            android:title="@string/action_contacts"
            app:showAsAction="always" />
    
        <item
            android:id="@+id/action_settings"
            android:title="@string/action_settings"
            app:showAsAction="never" />
    </menu>
    

    希望对你有帮助!

    【讨论】:

    • 那不只是将图标颜色改为黑色吗?
    • 它只是将图标而不是原始图标染成黑色。当您有一个颜色图标并且您只想采用其他颜色时,它将为您提供帮助。希望对你有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2012-09-18
    相关资源
    最近更新 更多