如何从Xamarin Forms Android Project中的默认白色更改BarTextColor?

时间:2019-04-10 13:53:53

标签: android xamarin

创建了一个新的Xamarin项目,并尝试更改其中的导航栏背景颜色(BarBackgroundColor)和文本颜色(BarTextColor)的颜色。似乎只有BarBackgroundColor有效,而BarTextColor无效。以下是App.xaml中的资源。

        <Color x:Key="digiGreen">#ADC710</Color>
        <Color x:Key="whiteColor">#FFFFFF</Color>        

     <Style TargetType="NavigationPage" ApplyToDerivedTypes="True">
        <!-- navigation bar to have digiGreen color -->
        <Setter Property="BarTextColor"
                Value="{StaticResource digiGreen}"/>
        <Setter Property="BarBackgroundColor" 
                Value="{StaticResource whiteColor}"/>
    </Style>

我当前使用的Xamarin版本是4.12.xx,模拟器是Android 9.0-API 28

2 个答案:

答案 0 :(得分:1)

  

如何从Xamarin中的默认白色更改BarTextColor   表单Android项目?

我建议您使用以下代码来设置NavigationPage属性:

public App()
{
    InitializeComponent();

    MainPage = new NavigationPage(new MainPage());

    ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Black;
    ((NavigationPage)Application.Current.MainPage).BarTextColor = Color.OrangeRed;
}

一旦您想更改特定页面的颜色,请在页面构造器中进行更改:

public Page1 ()
{
    InitializeComponent ();

    ((NavigationPage)Application.Current.MainPage).BarBackgroundColor = Color.Yellow;
    ((NavigationPage)Application.Current.MainPage).BarTextColor = Color.Red;
}

注意:请不要在MainPage的构造函数中对其进行更改,因为那时您无法获得Application.Current.MainPage

在xaml中,我在Android 9.0中进行了测试,而xamarin.forms的版本为3.4.0.1008975。我使用了您的代码,它就对我有效。

更新

要更改toolbaritem的TextColor: 转到Android Project-> Resources-> values-> styles.xml,然后添加:

<item name="android:actionMenuTextColor">#ADC710</item>

要更改back arrow的TextColor:

转到Android Project-> Resources-> values-> styles.xml,添加:

<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>

样式是:

  <style name="DrawerArrowStyle" parent="@style/Widget.AppCompat.DrawerArrowToggle">
    <item name="spinBars">true</item>
    <item name="color">#ADC710</item>
  </style>

BarTextColor仅会影响Android中Navigationbar.title的颜色。由于平台不同而有所不同。

引用:change-navigation-bar-back-button-color-in-xamarin-android

答案 1 :(得分:0)

要更改NavigationPage的BarTextColor和BarBackgroundColor,您可以: 在app.xaml中,然后在下面添加代码:

<ResourceDictionary>
            <Style TargetType="NavigationPage">
                <Setter Property="BarBackgroundColor" Value="White"/>
                <Setter Property="BarTextColor" Value="Black"/>
            </Style>
        </ResourceDictionary>