自定义TabbedPage颜色方案 - Xamarin.Forms

4
有没有办法定制Xamarin.Forms.TabbedPage对象选项卡的颜色模式,以便它不采用目标平台的默认外观和感觉?我想要更改字体颜色,背景颜色和当前选中选项卡的颜色。
6个回答

7
我建议使用自定义渲染器。
以下是iOS的示例:
[assembly: ExportRenderer(typeof(TabbedPage), typeof(TabbedPageRenderer))]
namespace MyApp.iOS
{
    public class TabbedPageRenderer : TabbedRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            TabBar.TintColor = UIColor.White;
            TabBar.BarTintColor = UIColor.Black;
            TabBar.BackgroundColor = UIColor.Gray;
        }
    }
}

在 Xamarin.iOS 项目中,只需将此类粘贴即可。

对于 Xamarin.Android,你也可以使用自定义渲染器来实现相同的功能。 Android 版本的自定义渲染器与 iOS 版本不同。


我该如何在Android上实现它? - Fran_gg7

4

1
在选项卡页面中,为了改变Xamarin Forms中的标题颜色而不是Android本地颜色。
选项卡页面代码:
 class MainPage : TabbedPage
    {
        LoginManager app;

        public MainPage(LoginManager ilm)
        {

            app = ilm;
            Title = "Infrastructure";
            Icon = "server.png";            


            this.BarTextColor = Color.White;
            this.BarBackgroundColor = Color.Blue;


            Children.Add(new AssetsView());
            Children.Add(new ServiceView());

            ToolbarItem tbi = new ToolbarItem() {
                Text = "Logout",
                Order = ToolbarItemOrder.Secondary,
                Priority = 0,       



            };

AssetView 代码:

 public AssetView()
        {
            Title = "Assets";           



           this.BackgroundColor = Color.FromHex("D3D3D3");

            list = new AssetsList();

            searchbar = new SearchBar()
            {

                Placeholder = "Search",
                TextColor = Color.Black,
                BackgroundColor = Color.White,
                CancelButtonColor = Color.Black,
                PlaceholderColor = Color.Black


            };

ServiceView 代码:

  public class ServiceView : ContentPage
    {

        ServiceList list;
        SearchBar searchbar;


        public ServiceView()
        {
            Title = "Services";
            this.BackgroundColor = Color.FromHex("D3D3D3");

            list = new ServiceList();

            searchbar = new SearchBar()
            {
                Placeholder = "Search",
                TextColor = Color.Black,
                BackgroundColor = Color.White,
                CancelButtonColor = Color.Black,
                PlaceholderColor = Color.Black
            };

1
在Xamarin.Forms中没有内置的方法,但在特定于平台的项目中很容易实现。例如,在iOS上可以使用UIAppearance来实现。

0
我可以通过以下方式在Android上实现这一点:
将您的Current.MainPage转换为TabbedPage-这将使您能够设置属性。
((TabbedPage)Current.MainPage).BarBackgroundColor = Color.FromHex(settings.AppSecondaryColour);

enter image description here

您应该能够以同样的方式更改其他属性。我尚未在IOS上测试过此功能。


0
你可以这样做:
<TabbedPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:d="http://xamarin.com/schemas/2014/forms/design"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
            mc:Ignorable="d"
            xmlns:views="clr-namespace:SilCoyLuhn.Views"
            x:Class="SilCoyLuhn.Views.MainPage"
            BarBackgroundColor="{StaticResource Primary}"
            BarTextColor="{StaticResource LightTextColor}">

    <TabbedPage.Resources>
        <ResourceDictionary>
            <Color x:Key="Primary">#9DD69F</Color>
            <Color x:Key="Accent">#E1F4E0</Color>
            <Color x:Key="LightTextColor">#999999</Color>
        </ResourceDictionary>
        </TabbedPage.Resources>
</TabbedPage>

<TabbedPage.Resources> 中,我定义了静态资源,用作 BarBackgroundColorBarTextColor

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接