因此,我的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());
}
}
答案 0 :(得分:0)
不使用自定义渲染器就可以尝试的一件事是创建自己的导航工具栏。例如,您创建一个StackLayout myCustonNavbar;
,在其中插入所需的所有元素(如特定颜色的后退按钮),然后可以使用NavigationPage.SetTitleView(this, myCustonNavbar)
答案 1 :(得分:0)
您可以使用下面的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)]
原始后退按钮的颜色为白色:
我将后退按钮的颜色设置为蓝色: