【发布时间】:2014-09-24 02:08:13
【问题描述】:
有没有办法可以自定义Xamarin.Forms.TabbedPage 对象上选项卡的颜色架构,使其不采用目标平台的默认外观?
我想更改字体颜色、背景和当前选择的标签颜色。
【问题讨论】:
标签: xamarin.ios xamarin xamarin.android xamarin.forms tabbedpage
有没有办法可以自定义Xamarin.Forms.TabbedPage 对象上选项卡的颜色架构,使其不采用目标平台的默认外观?
我想更改字体颜色、背景和当前选择的标签颜色。
【问题讨论】:
标签: xamarin.ios xamarin xamarin.android xamarin.forms tabbedpage
我建议使用自定义渲染器。
以下是 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 版本不同。
【讨论】:
聚会迟到了。
现在您可以按如下方式更改标签页背景颜色
BarBackgroundColor = Color.Black;
以下链接可能对您有更多帮助
How to change color of tabbed page indicator in Xamarin.Droid?
http://thatcsharpguy.com/post/platformtabbedpage-xamarin-forms-en/
【讨论】:
Xamarin.Forms 中没有内置方法,但在特定于平台的项目中很容易做到这一点。例如通过在iOS 上使用 UIAppearance。
【讨论】:
在标签页中,为了在 xamarin 表单中更改标题颜色,而不是在 android native 中。
标签页代码:
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,
};
资产查看代码:
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
};
服务查看代码:
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
};
【讨论】:
你可以这样做:
<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> 中,我定义了用作BarBackgroundColor 和BarTextColor 的静态资源。
【讨论】: