You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MAUI Android平台ToolbarItem图标着色颜色修改方案咨询

Android平台MAUI ToolbarItem图标颜色修改方案

方法一:修改Android项目主题样式

直接在Android项目资源文件中配置Toolbar图标着色,无需额外业务代码:

  1. 打开Android项目下的Resources/values/styles.xml文件
  2. 找到当前应用使用的主题(通常是Theme.Maui.NoActionBar或自定义主题)
  3. 在主题内添加Toolbar样式配置,指定图标 tint 颜色:
<style name="MyTheme" parent="Theme.Maui.NoActionBar">
    <!-- 保留原有主题配置 -->
    <item name="toolbarStyle">@style/MyToolbarStyle</item>
</style>

<style name="MyToolbarStyle" parent="Widget.MaterialComponents.Toolbar">
    <item name="android:tint">@color/white</item> <!-- 设置目标颜色,比如白色 -->
</style>
  1. 确保Resources/values/colors.xml中存在对应颜色定义:
<color name="white">#FFFFFF</color>

方法二:使用Android平台自定义渲染器

通过自定义ToolbarItem渲染器,直接设置图标颜色:

  1. 在MAUI项目的Android平台目录下创建渲染器类:
using Android.Content;
using Android.Graphics;
using Microsoft.Maui.Controls.Platform.Android;
using Microsoft.Maui.Controls;
using YourAppNamespace.Platforms.Android;

[assembly: ExportRenderer(typeof(ToolbarItem), typeof(CustomToolbarItemRenderer))]
namespace YourAppNamespace.Platforms.Android
{
    public class CustomToolbarItemRenderer : ToolbarItemRenderer
    {
        public CustomToolbarItemRenderer(Context context) : base(context)
        {
        }

        protected override void OnElementChanged(ElementChangedEventArgs<ToolbarItem> e)
        {
            base.OnElementChanged(e);
            
            if (Control != null && e.NewElement != null)
            {
                // 设置图标颜色为白色
                Control.SetColorFilter(Color.White);
            }
        }
    }
}
  1. 将代码中的YourAppNamespace替换为你的项目命名空间,所有ToolbarItem图标会被统一着色。

方法三:改用FontImageSource(允许替换图标资源时)

如果能将PNG图标换成字体图标,可更灵活控制颜色:
修改XAML中的ToolbarItem,使用FontImageSource并指定Color属性:

<ContentPage.ToolbarItems>
    <ToolbarItem 
        x:Name="SignReport"
        Command="{Binding SignReportCommand}">
        <ToolbarItem.IconImageSource>
            <FontImageSource 
                FontFamily="YourFontFamily" 
                Glyph="&#xe900;" <!-- 对应图标的Unicode编码 -->
                Color="White"/>
        </ToolbarItem.IconImageSource>
    </ToolbarItem>
</ContentPage.ToolbarItems>

以上三种方案均无需自定义Shell.TitleView,可直接实现Android平台ToolbarItem图标颜色修改。

内容的提问来源于stack exchange,提问作者ErnestJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 22:48:23