NavigationPage BarBackgroundColor在Xamarin.Android上不起作用

时间:2018-10-15 07:08:14

标签: xamarin xamarin.forms xamarin.android

我有XF应用程序,通过单击按钮即可为用户提供从浅色到深色主题的选项。我正在使用以下代码:

<ResourceDictionary xmlns="http://xamarin.com/schemas/2014/forms" 
                    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
                    x:Class="Japanese.NavBackgroundRes">
    <Style TargetType="NavigationPage">
        <Setter Property="BarBackgroundColor" Value="{DynamicResource GridBackgroundColor}"/>
        <Setter Property="BarTextColor" Value="{DynamicResource LabelTextColor}" />
    </Style>
</ResourceDictionary>

这在iOS上完美运行,但在Android上却不行。 BarTextColor属性适用于iOS和Android,但BarBackgroundColor仅适用于iOS。有人知道为什么吗?如果有帮助,我正在物理设备上调试。

2 个答案:

答案 0 :(得分:0)

Android中有一个名为Material design的概念。

由于Xamarin在Xamarin.Android中采用了本机Java Android行为,因此发生的情况是Android应用程序在其styles.xml文件中选择了主题,然后使用该样式设置条形背景颜色。

但是,当然有解决方法。无论您需要在Android方面进行哪些更改,您都必须在样式文件中对其进行更新,例如:

 <?xml version="1.0" encoding="utf-8" ?>
 <resources>
 <style name="MyTheme" parent="MyTheme.Base">
   </style>

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

 <style name="MyTheme.Base" parent="Theme.AppCompat.Light.DarkActionBar">
 <item name="windowNoTitle">true</item>
 <item name="windowActionBar">false</item>
 <item name="colorPrimary">#003399</item>
 <item name="colorPrimaryDark">#003399</item>
 <item name="colorControlHighlight">#003399</item>
 <item name="colorAccent">#012348</item>
 <item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
 </style>

 </resources>

这里的颜色更改将直接反映在那里,例如ColorPrimary是您的工具栏背景颜色(BarBackgroundColor)。

更新

 <?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:id="@+id/toolbar"
 android:minHeight="?attr/actionBarSize"
 android:background="?attr/colorPrimary"
 app:theme="@style/ToolbarTheme" >
</android.support.v7.widget.Toolbar>

然后获得工具栏,如下所示:

var toolbar=yourActivityContext.Window.DecorView.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
toolbar.SetBackgroundColor(Android.Graphics.Color.Your_Color);
//In case of hex color 
toolbar.SetBackgroundColor(Android.Graphics.Color.ParseColor("#ebebeb"));

答案 1 :(得分:0)

在Android中使用BackgroundColor而不是BarBackgroundColor。

同时使用android和iPhone的这两个属性。

<NavigationPage BarBackgroundColor="#F05123" BackgroundColor="#F05123" BarTextColor="White" >
相关问题