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

时间:2019-03-25 11:05:40

标签: android android-layout android-styles android-appbarlayout

我想做一些非常简单的事情:更改整个主题的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

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>

4 个答案:

答案 0 :(得分:1)

在高于1.0.0的android依赖的google物料实现的更高版本中,他们添加了appBarLayoutStyle。当我问这个问题时,我正在使用该版本。我想现在这是没有意义的,但是是...解决方案是升级到材料设计库的较新版本,并在<style name="AppTheme">

中使用customizable属性

答案 1 :(得分: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是红色,将其更改为所需的任何颜色。

答案 2 :(得分:0)

您拥有<View style={{ flexDirection: 'row', flexWrap:'wrap' }}> {this.renderImages()} </View> ,因此将其设为BaseTheme的父项。 因此,您要应用从DefaultAppBarLayoutDefaultAppBarLayout的更改。

代码:

BaseTheme

(还没有尝试过这种方法,但我希望它能起作用)

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

答案 3 :(得分:0)

我在appBarLayoutStyle类中看到了AppBarLayout属性,但是我不知道这是否是您要的内容。如果此属性不起作用,一种解决方案是为所有AppBarLayout创建样式,指定android:background属性,然后在每个style="@style/YourAppBarLayoutStyle"中应用AppBarLayout