【问题标题】:Change back button color in Xamarin.Android更改 Xamarin.Android 中的后退按钮颜色
【发布时间】:2020-10-28 21:51:06
【问题描述】:

所以,我的 Xamarin 应用程序中有一个工具栏,我想更改 Android 应用程序的后退按钮的颜色(我认为 iOS 应用程序的蓝色适合我的背景)。

我知道已经有人问过这个问题,但是这些解决方案都不适合我。 (我用https://xamgirl.com/transparent-navigation-bar-in-xamarin-forms/ 将工具栏更改为透明,也许这很重要。)

谁能帮我解决这个问题?

CustomNavigationPage.cs

using Xamarin.Forms;

namespace TransparentNavBarXForms
{
    public partial class CustomNavigationPage : NavigationPage
    {
        public CustomNavigationPage() : base()
        {
            InitializeComponent();
        }

        public CustomNavigationPage(Page root) : base(root)
        {
            InitializeComponent();
        }

        public bool IgnoreLayoutChange { get; set; } = false;

        protected override void OnSizeAllocated(double width, double height)
        {
            if (!IgnoreLayoutChange)
                base.OnSizeAllocated(width, height);
        }
    }
}

CustomNavigationPage.xaml

<?xml version="1.0" encoding="utf-8" ?>
<NavigationPage
    xmlns="http://xamarin.com/schemas/2014/forms"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    x:Class="TransparentNavBarXForms.CustomNavigationPage"
    xmlns:iOS="clr-namespace:Xamarin.Forms.PlatformConfiguration.iOSSpecific;assembly=Xamarin.Forms.Core"
    iOS:NavigationPage.IsNavigationBarTranslucent="True"
    BarTextColor="White">
    <NavigationPage.BarBackgroundColor>
        <OnPlatform x:TypeArguments="Color">
            <On Platform="Android, iOS" Value="Transparent" />
        </OnPlatform>
    </NavigationPage.BarBackgroundColor>
</NavigationPage>

iOSCustomNavigationRenderer

using System.ComponentModel;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;
using UIKit;
using System;
using TransparentNavBarXForms;
using TransparentNavBarXForms.iOS.Renderers;

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(CustomNavigationRenderer))]
namespace TransparentNavBarXForms.iOS.Renderers
{
    public class CustomNavigationRenderer : NavigationRenderer
    {
        public override void ViewDidLoad()
        {
            base.ViewDidLoad();

            UINavigationBar.Appearance.SetBackgroundImage(new UIImage(), UIBarMetrics.Default);
            UINavigationBar.Appearance.ShadowImage = new UIImage();
            UINavigationBar.Appearance.BackgroundColor = UIColor.Clear;
            UINavigationBar.Appearance.TintColor = UIColor.White;
            UINavigationBar.Appearance.BarTintColor = UIColor.Clear;
            UINavigationBar.Appearance.Translucent = true;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
            }

            base.Dispose(disposing);
        }
    }
}

CustomNavigationPageRenderer

using Android.Support.V7.Widget;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
using Xamarin.Forms.Platform.Android.AppCompat;
using AView = Android.Views.View;
using Android.App;
using Android.Content;
using Android.Widget;
using TransparentNavBarXForms;
using TransparentNavBarXForms.Droid.Renderers;

[assembly: ExportRenderer(typeof(CustomNavigationPage), typeof(CustomNavigationPageRenderer))]
namespace TransparentNavBarXForms.Droid.Renderers
{
    public class CustomNavigationPageRenderer : NavigationPageRenderer
    {
        public CustomNavigationPageRenderer(Context context) : base(context)
        {

        }

        IPageController PageController => Element as IPageController;
        CustomNavigationPage CustomNavigationPage => Element as CustomNavigationPage;

        protected override void OnLayout(bool changed, int l, int t, int r, int b)
        {
            CustomNavigationPage.IgnoreLayoutChange = true;
            base.OnLayout(changed, l, t, r, b);
            CustomNavigationPage.IgnoreLayoutChange = false;

            int containerHeight = b - t;

            PageController.ContainerArea = new Rectangle(0, 0, Context.FromPixels(r - l), Context.FromPixels(containerHeight));

            for (var i = 0; i < ChildCount; i++)
            {
                AView child = GetChildAt(i);

                if (child is Android.Support.V7.Widget.Toolbar)
                {
                    continue;
                }

                child.Layout(0, 0, r, b);
            }
        }
    }
}

App.xaml.cs

public partial class App : Application
    {
        public App()
        {
            InitializeComponent();

            MainPage = new CustomNavigationPage(new MainPage());
        }
    }

【问题讨论】:

  • 请包含一些代码作为起点。不要将您的代码发布为链接或图像,而是将其格式化为带有三个反引号``` 的代码块。来自review
  • 这不是我的代码。这是来自网站的教程。
  • 无论如何,在这个页面上添加必要的代码。如果教程网站更改代码,以后的读者将无法使用它。
  • 给你,我添加了代码。

标签: c# android visual-studio xamarin xamarin.android


【解决方案1】:

您可以尝试不使用自定义渲染器的一件事是创建自己的导航工具栏。例如,您创建一个StackLayout myCustonNavbar;,在其中插入您想要的所有元素(例如特定颜色的后退按钮),然后您可以使用NavigationPage.SetTitleView(this, myCustonNavbar) 将其设置为自定义工具栏

【讨论】:

    【解决方案2】:

    您可以使用下面的 style.xml 来更改后退按钮的颜色。

     <style name="MainTheme.Base2" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
    </style>
    <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">#2196F3</item>
    

    在 MainActivity 中更改主题:

       [Activity(Label = "XamarinDemo", Icon = "@mipmap/icon", Theme = "@style/MainTheme.Base2", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    

    Thr 原后退按钮颜色为白色:

    我将后退按钮颜色设置为蓝色:

    【讨论】: