简体   繁体   English

更改 Xamarin.Android 中的后退按钮颜色

[英]Change back button color in Xamarin.Android

So, I've got a toolbar in my Xamarin app and I want to change the color of the back button of the Android app (I think the blue of the iOS app would work fine with my background).所以,我的 Xamarin 应用程序中有一个工具栏,我想更改 Android 应用程序的后退按钮的颜色(我认为 iOS 应用程序的蓝色适用于我的背景)。

I know that this question has already been asked, but none of those solutions worked for me.我知道这个问题已经被问过了,但是这些解决方案都没有对我有用。 (I changed the toolbar to transparent with https://xamgirl.com/transparent-navigation-bar-in-xamarin-forms/ Maybe that's important.) (我使用https://xamgirl.com/transparent-navigation-bar-in-xamarin-forms/将工具栏更改为透明,也许这很重要。)

Could anyone help me with this?谁能帮我解决这个问题?

CustomNavigationPage.cs 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 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 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 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 App.xaml.cs

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

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

One thing that you could try without using custom renderers is to create your own navigation toolbar.不使用自定义渲染器可以尝试的一件事是创建自己的导航工具栏。 For example, you create a StackLayout myCustonNavbar;例如,您创建一个StackLayout myCustonNavbar; in which you insert all the elements that you want (like the back button of a particular color) and than you can set it as a custom toolbar with NavigationPage.SetTitleView(this, myCustonNavbar)在其中插入所需的所有元素(例如特定颜色的后退按钮),然后可以使用NavigationPage.SetTitleView(this, myCustonNavbar)将其设置为自定义工具栏

You could use the style.xml below to change the back button color.您可以使用下面的 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>

Change the Theme in MainActivity:在 MainActivity 中更改主题:

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

Thr original back button color is white: Thr 原后退按钮颜色为白色:

在此处输入图像描述

I set the back button color to blue:我将后退按钮颜色设置为蓝色:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM