xamarin.froms ActionBar 标题栏图标图片颜色显示不出来,无论什么颜色的图片显示出来都是白色,求解决方案

里面用的是ToolbarItem控件放了个图片,但是无论图片什么颜色,显示出来的始终还是白色,怎么怎么才能把颜色显示出来?

img

引用chatGPT作答,如果您在 Xamarin.Froms 应用程序中使用 ActionBar 并且标题栏图标颜色无法正确显示,可能有以下解决方案:

1.使用白色图标
如果您的应用程序主题为深色,则可能需要将 ActionBar 标题栏的图标设置为白色,以使其在深色背景上正确显示。您可以在应用程序的资源文件夹中添加一个白色版本的图标,并在代码中将其设置为 ActionBar 标题栏的图标。

2.将应用程序主题设置为浅色
如果您不想使用白色图标,则可以尝试将应用程序主题设置为浅色。这将确保 ActionBar 标题栏的图标颜色正确显示。您可以在应用程序的样式文件中设置主题,例如:

<style name="AppTheme" parent="Theme.AppCompat.Light">
  <!-- Set other theme attributes -->
  <item name="android:windowBackground">@android:color/white</item>
</style>

3.使用 AppCompat 库
如果您使用的是旧版的 ActionBar,并且无法正确显示图标颜色,则可以尝试使用 AppCompat 库。这将确保 ActionBar 标题栏的图标颜色正确显示。要使用 AppCompat 库,请在您的应用程序的 build.gradle 文件中添加以下依赖项:

dependencies {
    implementation 'com.android.support:appcompat-v7:28.0.0'
}

然后,您可以在代码中使用 AppCompat ActionBar 类,例如:

using Android.Support.V7.App;

// ...

public class MainActivity : AppCompatActivity
{
    // ...

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set the action bar
        SupportActionBar.SetDisplayHomeAsUpEnabled(true);
        SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.my_icon);
    }

    // ...
}

希望这些解决方案能够帮助您解决问题。

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
在 Xamarin.Forms 中,可以使用自定义的 Renderer 实现 ActionBar 标题栏图标图片颜色的显示。下面是一份解决方案:

  1. 创建一个自定义的 ToolbarItem 类,用于控制标题栏的样式和行为。其中,我们可以添加一个名为 “IconColor” 的属性,让用户可以指定图标的颜色:
public class CustomToolbarItem : ToolbarItem
{
    public static readonly BindableProperty IconColorProperty = BindableProperty.Create(nameof(IconColor), typeof(Color), typeof(CustomToolbarItem), Color.Default);

    public Color IconColor
    {
        get { return (Color)GetValue(IconColorProperty); }
        set { SetValue(IconColorProperty, value); }
    }
}
  1. 创建自定义的 Renderer,用于在 Android 平台上控制 ToolbarItem。在 Renderer 类中,我们可以改变图标显示的颜色:
[assembly: ExportRenderer(typeof(CustomToolbarItem), typeof(CustomToolbarItemRenderer))]

namespace MyApp.Droid.Renderers
{
    public class CustomToolbarItemRenderer : ToolbarItemRenderer
    {
        protected override void OnElementChanged(ElementChangedEventArgs<ToolbarItem> e)
        {
            base.OnElementChanged(e);

            if (Control != null)
            {
                var item = e.NewElement as CustomToolbarItem;
                if (item != null)
                {
                    var icon = item.IconImageSource as FileImageSource;
                    if (icon != null)
                    {
                        var drawable = Resources.GetDrawable(icon.File);

                        if (item.IconColor != Color.Default)
                        {
                            drawable.SetColorFilter(item.IconColor.ToAndroid(), PorterDuff.Mode.SrcIn);
                        }

                        Control.SetIcon(drawable);
                    }
                }
            }
        }
    }
}
  1. 在 XAML 文件中使用自定义 ToolbarItem 控件,并设置 “IconColor” 属性的值。例如,在 MainPage.xaml 中,添加以下代码段:
<ContentPage.ToolbarItems>
    <local:CustomToolbarItem Order="Primary" Priority="0" Icon="my_icon.png" IconColor="Red" Clicked="OnToolbarItemClicked" />
</ContentPage.ToolbarItems>

在上面的代码中,“Icon” 属性指定了要显示的图标的文件名,“IconColor” 属性指定了图标的颜色。你可以将 “IconColor” 属性设置为任何有效的颜色值。

以上就是解决方案的全部内容。通过这种方式,你可以在 Xamarin.Forms 中轻松地实现 ActionBar 标题栏图标图片颜色的显示。
如果我的回答解决了您的问题,请采纳!