在我的应用中实现深色主题;不同活动的工具栏中显示不同的颜色 [英] implementing dark theme in my app; different color is showing in toolbar of different activity

查看:134
本文介绍了在我的应用中实现深色主题;不同活动的工具栏中显示不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的应用程序中实现深色主题;它在mainactivity中有一个工具栏,在其他activity中有一个动作栏.我在values/styles.xml中实现了深色主题,如下所示:

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.

在我的主要活动中,有recyclerview制作的cardview;从这张图片开始,我想实现深色主题.

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.

我也想将cardview布局颜色的整个背景颜色设置为给定的图片. 我尝试了很多次,但是没有发生背景闪烁:(

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 :(

顺便说一下,这是我的工具栏的xml(app_bar_main.xml):

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>

推荐答案

使用:

<com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/MyAppBar"
        ..>

其中:

<style name="MyAppBar" parent="@style/Widget.Design.AppBarLayout">
    <item name="android:background">?attr/colorPrimary</item>
    <item name="android:elevation">xxdp</item>
    ...
</style>

这篇关于在我的应用中实现深色主题;不同活动的工具栏中显示不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆