问题
I wanted to implement dark theme in my app; it has a toolbar in mainactivity and action bar in other activity. I implemented dark theme in values/styles.xml as below:
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents">
<item name="colorPrimary">@color/primary_material_dark</item>
<item name="colorPrimaryDark">@color/primary_dark_material_dark</item>
<item name="colorAccent">@color/material_deep_teal_200</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay"parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
After doing this, my app became in dark theme but the color of toolbar of main activity is diiferent and color of action bar of other activity are different. And, there is also a kind of blink while going to another activity.
In my main activity there are cardview made by recyclerview; I wanted to implement dark theme as of this picture.
I don't know why the color of toolbar is changing while going from main activity to other activity(other activity have actionbar) and the background is blinking while going from one activity to another.
I also want to set the color of entire background of layout color of cardview as given picture. I tried to do this so many time but it is not happening the background is blinking :(
By the way, this is my toolbar's xml(app_bar_main.xml):
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
..
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
/>
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
<com.google.android.material.floatingactionbutton.FloatingActionButton.../>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
回答1:
Use:
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="@style/MyAppBar"
..>
where:
<style name="MyAppBar" parent="@style/Widget.Design.AppBarLayout">
<item name="android:background">?attr/colorPrimary</item>
<item name="android:elevation">xxdp</item>
...
</style>
来源:https://stackoverflow.com/questions/58589869/implementing-dark-theme-in-my-app-different-color-is-showing-in-toolbar-of-diff