基于主题属性设置工具栏背景颜色会使应用程序崩溃

时间:2015-08-03 09:29:32

标签: android colors reference attributes toolbar

所以,我通过从设置中选择主题来设置工具栏的颜色......颜色应该是color_primary。 当我将“?attr / color_primary”设置为工具栏的背景时,应用程序崩溃 有这个错误: https://github.com/Heromine/tempapp1/issues/5 (我发布链接,因为我收到错误粘贴在这里错误,抱歉)

所以这里是带有主题的styles.xml:

<resources xmlns:tools="http://schemas.android.com/tools">


<!-- Activity themes -->

<style name="Dark" parent="Theme.AppCompat" />




<!-- Application themes. -->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!--
        Theme customizations available in newer API levels can go in
        res/values-vXX/styles.xml, while customizations related to
        backward-compatibility can go here.
    -->
    <item name="android:navigationBarColor">?attr/color_primary</item>
</style>



<!-- Application themes. -->
<style name="AppTheme.Brown" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <item name="color_primary_dark">@color/minecraft_brown_dark_dirt_light</item>
    <item name="color_primary">@color/minecraft_brown_dirt_darkest</item>
    <item name="color_accent">@color/minecraft_green_grass_lightest</item>
    <item name="color_drawer_divider">?attr/color_primary_dark</item>
    <item name="android:textColorLink">?attr/color_primary</item>
    <item name="color_preferences">@color/minecraft_brown_dirt_light</item>

    <!--   your app branding color for the app bar -->
    <item name="colorPrimary">?attr/color_primary</item>

    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">?attr/color_primary_dark</item>

    <!--   theme UI controls like checkboxes and text fields -->
    <item name="colorAccent">?attr/color_accent</item>

</style>


<style name="AppTheme.Green" parent="AppBaseTheme">
    <!-- All customizations that are NOT specific to a particular API-level can go here. -->

    <item name="color_primary_dark">@color/minecraft_green_cactus_darkest</item>
    <item name="color_primary">@color/minecraft_green_cactus_light</item>
    <item name="color_accent">@color/minecraft_brown_dirt_dark</item>
    <item name="color_drawer_divider">@color/minecraft_green_grass_lightest</item>
    <item name="android:textColorLink">?attr/color_primary</item>
    <item name="color_preferences">?attr/color_primary</item>

    <!--   your app branding color for the app bar -->
    <item name="colorPrimary">?attr/color_primary</item>

    <!--   darker variant for the status bar and contextual app bars -->
    <item name="colorPrimaryDark">?attr/color_primary_dark</item>

    <!--   theme UI controls like checkboxes and text fields -->
    <item name="colorAccent">?attr/color_accent</item>

</style>


<style name="PreferenceFragmentTheme" parent="AppBaseTheme">
    <item name="colorAccent">@color/minecraft_brown_dirt_light</item>
    <item name="android:textColor">@color/extracted_primary_text_default_material_light</item>
    <item name="android:textColorSecondary">@color/extracted_secondary_text_default_material_light</item>
</style>


           <!-- LOOK HERE -->
<style name="ToolBarStyle" parent="Widget.AppCompat.ActionBar">
    <item name="android:background">?attr/color_primary</item>
    <item name="theme">@style/ToolBarTheme</item>
    <item name="popupTheme">@style/ToolBarPopupTheme</item>
    <item name="titleTextAppearance">@style/ToolBarTitleTheme</item>
    <item name="android:elevation" tools:targetApi="21">@dimen/elevation_default</item>
</style>

<style name="ToolBarTheme" parent="ThemeOverlay.AppCompat.ActionBar">
    <item name="android:textColorPrimary">@color/toolbar_title_text_color</item>
</style>

<style name="ToolBarTitleTheme" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
    <item name="android:textStyle">normal</item>
</style>

<style name="ToolBarPopupTheme" parent="ThemeOverlay.AppCompat.Dark">
    <item name="android:colorBackground">@color/minecraft_green_cactus_light</item>
</style>



<style name="CardViewStyle" parent="CardView.Light">
    <item name="cardUseCompatPadding">true</item>
    <item name="cardBackgroundColor">@color/white</item>
    <item name="cardElevation">4dp</item>
    <item name="cardCornerRadius">4dp</item>
</style>



<style name="ButtonTransparentStyle" parent="android:Widget.DeviceDefault.Button.Borderless">
    <item name="android:textColor">@color/extracted_secondary_text_default_material_light</item>
    <item name="android:textAllCaps">true</item>
    <item name="android:textAppearance">?android:attr/textAppearanceButton</item>
</style>

attrs.xml:

<?xml version="1.0" encoding="utf-8"?>

<attr name="color_primary" format="color|reference" />
<attr name="color_primary_dark" format="color|reference" />
<attr name="color_accent" format="color|reference" />
<attr name="color_drawer_divider" format="color|reference" />
<attr name="color_preferences" format="color|reference" />

color.xml

<resources>

<color name="window_background">#CC000000</color>
<color name="greenactbar">#00ff11</color>
<item name="blue" type="color">#FF33B5E5</item>
<item name="purple" type="color">#FFAA66CC</item>
<item name="green" type="color">#FF99CC00</item>
<item name="orange" type="color">#FFFFBB33</item>
<item name="red" type="color">#FFFF4444</item>
<item name="darkblue" type="color">#FF0099CC</item>
<item name="darkpurple" type="color">#FF9933CC</item>
<item name="darkgreen" type="color">#FF669900</item>
<item name="darkorange" type="color">#FFFF8800</item>
<item name="darkred" type="color">#FFCC0000</item>
<item name="black" type="color">#FF000000</item>
<item name="white" type="color">#FFFFFFFF</item>

<item name="minecraft_button_background_not_pressed" type="color">#FF958681</item>
<item name="minecraft_button_background_pressed" type="color">#FF716567</item>
<item name="minecraft_button_text" type="color">#FFDEDEDE</item>
<item name="minecraft_button_text_shadow" type="color">#FF393939</item>
<item name="minecraft_button_light" type="color">#FFBCB1AB</item>
<item name="minecraft_button_dark" type="color">#FF282729</item>

<item name="minecraft_brown_dark_dirt_light" type="color">#FF3A2A1E</item>
<item name="minecraft_brown_dirt_darkest" type="color">#FF583D2B</item>
<item name="minecraft_brown_dirt_dark" type="color">#FF78543C</item>
<item name="minecraft_brown_dirt_light" type="color">#FF956B4D</item>
<item name="minecraft_brown_dirt_lightest" type="color">#FFB88460</item>

<item name="minecraft_green_grass_darkest" type="color">#FF406129</item>
<item name="minecraft_green_grass_dark" type="color">#FF476D2D</item>
<item name="minecraft_green_grass_light" type="color">#FF537D35</item>
<item name="minecraft_green_grass_lightest" type="color">#FF679940</item>

<item name="minecraft_green_cactus_darkest" type="color">#FF116120</item>
<item name="minecraft_green_cactus_light" type="color">#FF17832A</item>
<item name="minecraft_green_cactus_lightest" type="color">#FF199130</item>


<!-- colors of element of the app -->
<color name="toolbar_title_text_color">@color/white</color>
<color name="tabs_indicator_color">@color/white</color>
<color name="tabs_selected_text_color">@color/white</color>
<color name="tabs_not_selected_text_color">@color/minecraft_button_light</color>
<color name="drawer_text_shadow_color" type="color">#FF292929</color>
<color name="text_changelog_color" type="color">#FFA5A5A5</color>

<!-- colors used for the Settings page, on pre-lollipop devices the colors were different -->
<color name="extracted_primary_text_default_material_light">#de000000</color>
<color name="extracted_secondary_text_default_material_light">#8a000000</color>
<color name="material_deep_purple_500">@color/purple</color>
<color name="lannister">@color/purple</color>
<color name="textBackground">#8a000000</color>



<integer-array name="androidcolors">
    <item>@color/blue</item>
    <item>@color/purple</item>
    <item>@color/green</item>
    <item>@color/orange</item>
    <item>@color/red</item>
    <item>@color/darkblue</item>
    <item>@color/darkpurple</item>
    <item>@color/darkgreen</item>
    <item>@color/darkorange</item>
    <item>@color/darkred</item>
    <item>@color/black</item>
    <item>@color/white</item>
</integer-array>



</resources>

布局:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/ToolBarStyle"
    android:id="@+id/toolbar"/>

<com.astuetz.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip" />

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentTop="true" />

</RelativeLayout>

我在Android 5.0.2上,在棒棒糖之前无需回答版本

1 个答案:

答案 0 :(得分:0)

您应该将工具栏的背景设置为您想要的颜色:

            <android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/your_primary_color"
android:id="@+id/toolbar"/>

或者你可以通过setBackground()方法以编程方式更改...不需要在这里使用attrs ...但是如果你需要使用attrs那么appcompat可以通过删除&来使用旧的Android版本#34;机器人:&#34;像这样的前缀:&#34;?attr / your_attr&#34;