动态更改Android工具栏/选项卡栏主题

动态更改Android工具栏/选项卡栏主题,android,xamarin.forms,Android,Xamarin.forms,我正在尝试在Android应用程序中动态更改工具栏主题。我有一个自定义的明暗主题/样式。它们在应用程序中动态更改,并且在大多数情况下,UI都会更新。正面和负面样式按钮会正确更新,但ActionBar主题不会更新。 styles.xml文件位于以下位置: <?xml version="1.0" encoding="utf-8" ?> <resources> <style name="MainTheme"

我正在尝试在Android应用程序中动态更改工具栏主题。我有一个自定义的明暗主题/样式。它们在应用程序中动态更改,并且在大多数情况下,UI都会更新。正面和负面样式按钮会正确更新,但ActionBar主题不会更新。 styles.xml文件位于以下位置:

<?xml version="1.0" encoding="utf-8" ?>
<resources>
    <style name="MainTheme" parent="Theme.AppCompat.DayNight.NoActionBar" />

    <!-- Light Themes -->
    <style name="DayTheme" parent="MainTheme">
        <item name="colorPrimary">@color/colorPrimary</item>"
    </style>

    <style name="DayTheme.ActionBar" parent="MainTheme">
        <item name="android:background">@color/colorPrimary</item>
        <item name="android:textColorPrimary">@color/whiteSmoke</item>
        <item name="android:homeAsUpIndicator">@color/whiteSmoke</item>
        <item name="colorControlNormal">@color/whiteSmoke</item>
    </style>

    <!-- Dark Themes -->
    <style name="PositiveButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">@color/colorPrimaryDark</item>
    </style>

    <style name="NegativeButtonStyle" parent="Widget.AppCompat.Button.ButtonBar.AlertDialog">
        <item name="android:textColor">#999999</item>
    </style>

    <style name="NightTheme" parent="MainTheme" >
        <item name="buttonBarPositiveButtonStyle">@style/PositiveButtonStyle</item>
        <item name="buttonBarNegativeButtonStyle">@style/NegativeButtonStyle</item>
        <item name="colorPrimary">@color/colorPrimaryDark</item>"
    </style>

    <style name="NightTheme.ActionBar" parent="MainTheme">
        <item name="android:background">@color/grayDark</item>
        <item name="android:textColorPrimary">@color/whiteSmoke</item>
        <item name="android:homeAsUpIndicator">@color/whiteSmoke</item>
        <item name="colorControlNormal">@color/whiteSmoke</item>
    </style>
</resources>

@颜色/主色“
@颜色/原色
@彩色/白烟
@彩色/白烟
@彩色/白烟
@颜色/原色暗
#999999
@样式/正按钮样式
@样式/负片按钮样式
@颜色/原色深“
@颜色/灰色/深灰色
@彩色/白烟
@彩色/白烟
@彩色/白烟
我不确定如何在Toolbar.xml文件中引用它



具体来说,我在Xamarin表单应用程序中执行此操作,但是这个问题确实适用于特定于Android的应用。

尝试使用
com.google.Android.material.appbar.MaterialToolbar
而不是
Android.support.v7.widget.Toolbar
,可能会有所帮助。

尝试使用
com.google.Android.material.appbar.MaterialToolbar
而不是
android.support.v7.widget.Toolbar
也许它会有所帮助。

很抱歉,我最终在Xamarin表单特定的解决方案中解决了这个问题。我已经在Xamarin表单中自定义了对AppThemeBinding的引用,因此我可以使用以下内容设置导航页面的主题:

<Style TargetType="NavigationPage">
    <Setter Property="BarBackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource GrayDark}}" />
    <Setter Property="BarTextColor" Value="{StaticResource WhiteSmoke}" />
</Style>

谢谢你。 我将Toolbar.xml布局保持为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


我知道这不是一个Android解决方案,但如果其他人正在使用Xamarin表单,希望这会有所帮助:)

很抱歉,我最终在Xamarin表单特定的解决方案中解决了这个问题。我已经在Xamarin表单中自定义了对AppThemeBinding的引用,因此我可以使用以下内容设置导航页面的主题:

<Style TargetType="NavigationPage">
    <Setter Property="BarBackgroundColor" Value="{AppThemeBinding Light={StaticResource Primary}, Dark={StaticResource GrayDark}}" />
    <Setter Property="BarTextColor" Value="{StaticResource WhiteSmoke}" />
</Style>

谢谢你。 我将Toolbar.xml布局保持为:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:popupTheme="@style/ThemeOverlay.AppCompat.Light" />


我知道这不是一个Android解决方案,但如果其他人正在使用Xamarin表单,我希望这会有所帮助:)

不幸的是,Xamarin表单无法使用它,我希望我不必走这条路,我认为这是一个简单的定制安卓解决方案,而不是使用材料。谢谢:)不幸的是,这对Xamarin表单不起作用,我希望我不必走这条路,我认为他们是一个简单的定制Android解决方案,而不是使用材料。不过谢谢你:)