Я пытаюсь динамически изменить тему панели инструментов в приложении для Android. У меня есть пользовательские темы/стили светлой и темной темы. Они динамически изменяются в приложении, и по большей части обновляется пользовательский интерфейс. Кнопки положительного и отрицательного стиля обновляются правильно, а тема 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>
Я не знаю, как сослаться на это в файле Toolbar.xml.
<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:theme = "@style/MainTheme"
android:popupTheme = "@style/ThemeOverlay.AppCompat.Dark" />
В частности, я делаю это в приложении Xamarin Forms, но эта проблема определенно подходит для Android.
Попробуйте использовать com.google.android.material.appbar.MaterialToolbar
вместо android.support.v7.widget.Toolbar
, возможно, это поможет.
Извините за этот вопрос, в итоге я решил это в решении, специфичном для Xamarin Forms. У меня уже есть пользовательская ссылка на AppThemeBinding в Xamarin Forms, поэтому я смог установить тему NavigationPage, используя:
<Style TargetType = "NavigationPage">
<Setter Property = "BarBackgroundColor" Value = "{AppThemeBinding Light = {StaticResource Primary}, Dark = {StaticResource GrayDark}}" />
<Setter Property = "BarTextColor" Value = "{StaticResource WhiteSmoke}" />
</Style>
Спасибо https://stackoverflow.com/a/45890741/13902576. Я сохранил макет 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 Forms, надеюсь, это поможет :)
К сожалению, это не будет работать с формами Xamarin, я надеялся, что мне не придется идти по этому пути, и думал, что это простое пользовательское решение для Android, а не использование материала. Однако, спасибо :)