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

6
我是一位可以提供翻译的助手。
我希望做一件非常简单的事情:为整个主题更改 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的样式吗?或者我应该将我的新样式应用于哪个<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>

这是我想要编写的代码;在不需要每次指定“背景颜色为白色”的情况下。

所需行为

以下是可以使用的代码,但我不想在XML中的每个AppBarLayout中都写上android: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>

改成什么? - 007
将AppBarLayout样式覆盖为指定的样式。 - Some random IT boy
4个回答

2
在谷歌材料设计实现安卓依赖库的一些高版本中,比如1.0.0以上,他们添加了appBarLayoutStyle。我在提问时使用的是该版本。我猜现在这样做有点毫无意义了,但是...解决方案是升级到材料设计库的新版本,并在你的<style name="AppTheme">中使用可自定义的属性。

0
我在 AppBarLayout 类中看到了 appBarLayoutStyle 属性,但我不确定它是否是你要找的。如果这个属性不起作用,那么一个解决方案是为你所有的 AppBarLayout 创建一个样式,指定 android:background 属性,并将 style="@style/YourAppBarLayoutStyle" 应用于每个 AppBarLayout

是的。但我只想知道是否存在一种可以在整个应用程序中设置主题的属性。就像“buttonStyle”或类似的东西。 - Some random IT boy
@SomerandomITboy 你尝试过使用 appBarLayoutStyle 属性吗? - Ricardo
这个问题发布时还没有可用的解决方案。周一回到工作岗位后会尝试解决。 - Some random IT boy

0

你有你的BaseTheme,所以将其设置为DefaultAppBarLayout的父级。 这样,你就可以将DefaultAppBarLayout的更改应用到BaseTheme上。

代码:

<style name="BaseTheme" parent="DefaultAppBarLayout"> <!-- 一些实际上并不重要的颜色属性... --> </style>

(还没有尝试过这种方法,但希望它能够正常工作)

编辑: 别忘了在清单文件中设置BaseTheme。


0
在你的清单文件中,确保添加默认样式BaseTheme。这将应用于应用程序中的每个活动。如果您想为不同的活动添加其他主题,请为该活动添加特定主题。
<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是红色,你可以把它改成你想要的任何颜色。


主题在应用程序和活动中都设置了,我还有其他自定义样式,运行得非常完美。我需要在主题级别上更改 AppBarLayout 组件的样式。但我不知道要覆盖主题的哪个属性。 - Some random IT boy
你能用图片和一些代码解释一下你确切想要的吗? - 007
如果您想创建自定义主题,请不要添加父级。因为父级会带来一些预定义的功能。制作一个没有父级的样式。然后将此样式作为父级添加到其他样式中。 - 007
我想要所有的父属性,除了那个我想要改变的。请查看我在问题中的编辑。 - Some random IT boy
在这种情况下,只需将您的主题中的主要颜色更改为白色或您想要的任何颜色即可。 - 007
问题在于,所有依赖于colorPrimary的其他组件(我希望它们依赖于它)都将变为白色,这将比指定AppBarLayout背景要麻烦得多。 - Some random IT boy

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接