如何使用渲染器更改Android选项卡的背景颜色,同时使用自定义渲染器添加一些填充 [英] How can I change the background color of the Android tab with a renderer, while using a custom renderer to add some padding

查看:52
本文介绍了如何使用渲染器更改Android选项卡的背景颜色,同时使用自定义渲染器添加一些填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我将底部标签栏的背景颜色设置如下:

In my application I set the bottom tab bar background color like this:

<Shell xmlns="http://xamarin.com/schemas/2014/forms" 
       xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
       x:Class="M.Views.AppShell"
       NavBarIsVisible="True"
       xmlns:local="clr-namespace:Memorise"
       BackgroundColor="{DynamicResource ShellBackgroundColor}"
       TabBarBackgroundColor="{DynamicResource TabBarBackgroundColor}" 
       TabBarDisabledColor="{DynamicResource TabBarDisabledColor}" 
       TabBarForegroundColor="{DynamicResource TabBarForegroundColor}" 
       TabBarTitleColor="{DynamicResource TabBarTitleColor}" 
       TabBarUnselectedColor="{DynamicResource TabBarUnselectedColor}"

我创建了一个自定义渲染器,以在Android的标签上添加一些侧面填充:

I create a custom renderer to add some side padding to the tabs on Android:

public class CustomShellRenderer : ShellRenderer
{
    public CustomShellRenderer(Context context)
        : base(context)
    {
    }

    protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
    {
        return new MarginedTabBarAppearance();
    }
}

public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
    public void Dispose()
    {
    }

    public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {
        if (App.devWidth == SIZE.L)
            bottomView.SetPadding(400, 0, 400, 0);
    }

    public void ResetAppearance(BottomNavigationView bottomView)
    {
    }
}

不幸的是,这还具有副作用,因为它将选项卡的背景色设置为白色.

Unfortunately this also has a side effect in that it sets the background color of the tab to white.

有人对我如何使用此渲染器有任何了解,并且也将背景色更改为与DynamicResource ShellBackgroundColor相同吗?

Does anyone have any knowledge of how I use this renderer and also change the background color to be the same as the DynamicResource ShellBackgroundColor?

带有渲染器的代码已被注释掉:

public class CustomShellRenderer : ShellRenderer
{
    public CustomShellRenderer(Context context)
        : base(context)
    {
    }

    //protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
    //{
    //    return new MarginedTabBarAppearance();
    //}
}

public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
    public void Dispose()
    {
    }

    public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {
        //if (App.devWidth == SIZE.L)
        //    bottomView.SetPadding(400, 0, 400, 0);
        //if (Application.Current.Resources["TabBarBackgroundColor"] is Xamarin.Forms.Color color1)
        //{
        //    bottomView.SetBackgroundColor(color1.ToAndroid());
        //}
    }

    public void ResetAppearance(BottomNavigationView bottomView)
    {
    }
}

带有渲染器的制表符栏已被注释掉:

**未注释渲染器的代码:**

** Code with renderers not commented out:**

public class CustomShellRenderer : ShellRenderer
{
    public CustomShellRenderer(Context context)
        : base(context)
    {
    }

    protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
    {
        return new MarginedTabBarAppearance();
    }
}

public class MarginedTabBarAppearance : IShellBottomNavViewAppearanceTracker
{
    public void Dispose()
    {
    }

    public void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {
        if (App.devWidth == SIZE.L)
            bottomView.SetPadding(400, 0, 400, 0);
        if (Application.Current.Resources["TabBarBackgroundColor"] is Xamarin.Forms.Color color1)
        {
            bottomView.SetBackgroundColor(color1.ToAndroid());
        }
    }

    public void ResetAppearance(BottomNavigationView bottomView)
    {
    }
}

未渲染的制表符栏:

Mihail建议的更新代码:

public class MarginedTabBarAppearance :    ShellBottomNavViewAppearanceTracker
{

public MarginedTabBarAppearance(IShellContext shellContext, ShellItem shellItem)
    : base(shellContext, shellItem)
{
}

public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
{
    base.SetAppearance(bottomView, appearance);

    bottomView.SetPadding(400, 0, 400, 0);

    if (Application.Current.Resources.ContainsKey("TabBarBackgroundColor") && 
        Application.Current.Resources["TabBarBackgroundColor"] is Color tabColor)
    {
        bottomView.SetBackgroundColor(tabColor.ToAndroid());
    }
}
}

推荐答案

在您的SetAppearance方法中,您可以像这样设置背景颜色:

In your SetAppearance method, you can set the background color like this:

bottomView.SetBackgroundColor(Android.Graphics.Color.Red);

但是,SetBackgroundColor需要一种类型为Android.Graphics.Color的颜色,我看到您希望从 DynamicResource 中获取该颜色,该颜色将返回一个Xamarin.Forms.Color.好处是,这两种颜色类别都可以轻松转换,因此我们只需要获取 DynamicResource 的当前值:

However, the SetBackgroundColor requires a color of type Android.Graphics.Color and I see that you wish to take the color from a DynamicResource, which will return a Xamarin.Forms.Color. The good thing is that both color classes are easily convertable so we'll only need to take the current value of the DynamicResource:

if (Application.Current.Resources["CustomTabBackgroundColor"] is Xamarin.Forms.Color color)
{
    bottomView.SetBackgroundColor(color.ToAndroid());
}

在这里,CustomTabBackgroundColor是您的颜色的键,并且color.ToAndroid()在这两个类之间进行转换(从Xamarin到Android的一个).

Here, CustomTabBackgroundColor is the key for your color and color.ToAndroid() does the convertion between the 2 classes (from Xamarin's to Android's one).

更新

由于问题的更新,我将添加样式/颜色重置的问题不是在这种情况下是由于背景变化,而是完全由于其他原因.设置新视图外观的方式(return new MarginedTabBarAppearance();)将重置所有默认样式.由于我们是通过接口实现所有内容的,而不是从基类派生的,因此我们无法从已经预设的值中获取任何东西.为了解决这个问题,我们需要更改MarginedTabBarAppearance类的实现方式:

Due to an update to the question, I'll add that the issue with the styles/colors resetting isn't in this case due to the backround change, but it's due to something else entirely. The way that a new view appearance is being set (return new MarginedTabBarAppearance();) resets all of the default styles. Since we are implementing everything from an interface and not deriving from the base class, we can't take anything from the already preset values. In order to fix that, we'll need to change the way the MarginedTabBarAppearance class is being implemented:

public class MarginedTabBarAppearance : ShellBottomNavViewAppearanceTracker
{
    public MarginedTabBarAppearance(IShellContext shellContext, ShellItem shellItem)
        : base(shellContext, shellItem)
    {
    }

    public override void SetAppearance(BottomNavigationView bottomView, IShellAppearanceElement appearance)
    {
        base.SetAppearance(bottomView, appearance);

        bottomView.SetPadding(400, 0, 400, 0);

        if (Application.Current.Resources.ContainsKey("TabBarBackgroundColor") && 
            Application.Current.Resources["TabBarBackgroundColor"] is Color tabColor)
        {
            bottomView.SetBackgroundColor(tabColor.ToAndroid());
        }
    }
}

此外,您还必须像这样更改CreateBottomNavViewAppearanceTracker方法:

Also, you will have to change your CreateBottomNavViewAppearanceTracker method like this:

protected override IShellBottomNavViewAppearanceTracker CreateBottomNavViewAppearanceTracker(ShellItem shellItem)
{
    return new MarginedTabBarAppearance(this, shellItem);
}

通过这种方式,我们将采用我们已经样式化的所有内容,并且只需更改所需的内容即可.

This way we'll take everything that we have already styled and we'll simply change what we'll need.

这篇关于如何使用渲染器更改Android选项卡的背景颜色,同时使用自定义渲染器添加一些填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆