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

问题描述 投票:1回答:2

我想做一些非常简单的事情:为整个主题更改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>sh我应用我的新风格?

EDIT 1

Current behavior

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

这是我想写的代码;没有指定每一次“背景颜色=白色”

Desired behavior

适用于它的代码;但我不想在我的XML中为每个android:background="@color/colorTrueWhite"编写AppBarLayout

<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>
android android-layout android-styles android-appbarlayout
2个回答
0
投票

你有你的BaseTheme,所以让它成为DefaultAppBarLayout的父母。所以你正在将DefaultAppBarLayout的变化应用到BaseTheme

码:

<style name="BaseTheme" parent="DefaultAppBarLayout"> <!-- Some color attributes that doesn't actually matter... --> </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是红色,将其更改为您想要的任何颜色。

© www.soinside.com 2019 - 2024. All rights reserved.