使用工具栏时设置操作栏背景

时间:2016-03-17 10:05:42

标签: java android android-actionbar

我在过去几周一直在学习android编程,我有一个问题。 使用Theme.Appcompat.Light.NoActionBar主题并在我的活动中设置工具栏时,如何配置操作栏的背景颜色以及状态栏颜色? 我想在xml中进行,而不是以编程方式进行。

使用

时出现以下错误
Theme.AppCompat.Light.DarkActionBar

作为自定义样式的父级。

This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

这对我有用:

<resources>
<!-- Base application theme. -->
<style name="CustomMaterialTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="actionModeBackground">@color/colorPrimary</item>
    <item name="windowActionModeOverlay">true</item>
</style>

3 个答案:

答案 0 :(得分:0)

尝试制作自定义主题或添加到您当前的主题:

list

希望它有所帮助。谢谢参考这里我发现了一个很棒的教程http://coderzpassion.com/android-working-with-material-design/

答案 1 :(得分:0)

您需要为应用设置主要样式。

转到res-&gt; values-&gt; styles.xml文件。

在主题中添加以下内容。

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/ColorPrimary</item>
        <item name="colorPrimaryDark">@color/ColorPrimaryDark</item>
        <item name="android:windowDrawsSystemBarBackgrounds">true</item>
        <item name="android:statusBarColor">@color/ColorPrimary</item>
        <item name="android:navigationBarColor">@color/ColorPrimary</item>
    </style>

您需要在color.xml文件中设置颜色,如下所示。

<resources>
    <color name="ColorPrimary">Your Color</color>
    <color name="ColorPrimaryDark">Your Color</color>
</resources>

已完成。

希望它会对你有所帮助。

答案 2 :(得分:0)

尝试使用此功能并提供自定义颜色

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_home"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:theme="@style/ToolbarHomeColor" />

        </android.support.design.widget.AppBarLayout>


    </android.support.design.widget.CoordinatorLayout>
相关问题