如何在整个主题中更改 AppBarLayout 的样式?

How to change style of AppBarLayout in the entire theme?

我想做一些非常简单的事情:更改整个主题的 AppBarLayout 组件的背景。

这是我的主题,我不知道要覆盖哪个属性:

<style name="BaseTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <!-- Some color attributes that doesn't actually matter... -->

</style>

这里是 AppBarLayout 的新主题,我想将其应用于上述主题:

<style name="DefaultAppBarLayout" parent="Widget.Design.AppBarLayout">
    <item name="android:background">@android:color/transparent</item>
</style>

我应该为 BaseTheme 继承另一个 parent 的样式吗?或者我应该将我的新样式应用到哪个 <item name="attribute_name"></item>

编辑 1

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

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/more_actionbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:title="@string/moremenu_fragment_title"/>
    </com.google.android.material.appbar.AppBarLayout>

这是我要写的代码;无需每次都指定 "background color = white"

适用的代码;但我不想在我的 XML

中为每个 AppBarLayoutandroid:background="@color/colorTrueWhite"
<com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorTrueWhite">

        <androidx.appcompat.widget.Toolbar
                android:id="@+id/more_actionbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:title="@string/moremenu_fragment_title"/>
    </com.google.android.material.appbar.AppBarLayout>

在您的清单中确保添加默认样式 BaseTheme 这将应用于您应用程序中的每个 activity。如果您想要其他 activity 的主题,请将特定主题添加到 activity.

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_logo"
    android:label="@string/app_name"
    android:roundIcon="@drawable/ic_logo"
    android:supportsRtl="true"
    android:theme="@style/BaseTheme"
    android:usesCleartextTraffic="true">

让你有这样的风格。

   <style name="BaseTheme" parent="DefaultAppBarLayout">
        <!-- Some color attributes that doesn't actually matter... -->
    </style>

    <style name="DefaultAppBarLayout" parent="Widget.Design.AppBarLayout">
        <item name="android:background">@android:color/transparent</item>
    </style>

在你的主题中添加这一行

<item name="colorPrimary">#FF0000</item>

#FF0000 是红色,可以改成你想要的颜色。

你有你的 BaseTheme,所以让它成为 DefaultAppBarLayout 的父级。 因此,您正在应用从 DefaultAppBarLayoutBaseTheme 的更改。

代码:

<style name="BaseTheme" parent="DefaultAppBarLayout"> <!-- Some color attributes that doesn't actually matter... --> </style>

(这个方法还没试过,希望有用)

编辑: 不要忘记在清单中设置 BaseTheme。

我在AppBarLayoutclass中看到了appBarLayoutStyle属性,但不知道是不是你要找的。如果此属性不起作用,解决方案是为所有 AppBarLayout 创建样式,指定 android:background 属性并在每个 AppBarLayout.[=16= 中应用 style="@style/YourAppBarLayoutStyle" ]

在 google material 实现的 1.0.0 更高的版本中,android 依赖项添加了 appBarLayoutStyle。当我问这个问题时,我正在使用那个版本。我想现在它有点毫无意义但是是的......解决方案是升级到更新版本的 material 设计库并在你的 <style name="AppTheme">

中使用可自定义的属性