我正在使用来自 http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html 的相同代码对于我的项目。
当我创建一个空白项目以仅测试工具栏时,颜色正常工作。但是,在使用相同的代码将我的项目升级到 Material Design 后,工具栏颜色变为灰色。
android:background?attr/colorPrimary
似乎没有加载正确的颜色。当我使用 @color\theme_red
时,工具栏上的颜色设置正确。
这里出了什么问题?
我的 colors.xml:
<?xml version="1.0" encoding="utf-8"?><resources>
<color name="theme_red">#d43d1e</color>
<color name="theme_red_dark">#aa3118</color>
<color name="theme_accent">#3333ff</color>
</resources>
&样式.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/theme_red</item>
<item name="colorPrimaryDark">@color/theme_red_dark</item>
<item name="colorAccent">@color/theme_accent</item>
</style>
& 工具栏代码:
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
android:background?attr/colorPrimary
工具栏的样子:
使用 @color/theme_red
时工具栏的外观:
更新:
我正在更新问题,因为现在我在代码中有另一个似乎相关的错误。我在 Android 5.0.2 手机上试用了该应用程序,即使在 styles.xml 中定义了正确的主题和颜色,状态栏也没有染上深色。状态栏的颜色与第一张图片中的 Toolbar
完全相同。
styles.xml 在上面。 v-21/styles.xml 如下:
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="colorPrimary">@color/theme_red</item>
<item name="colorPrimaryDark">@color/theme_red_dark</item>
<item name="colorAccent">@color/theme_accent</item>
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
不知何故,即使正确定义了颜色,系统也无法使用这些颜色。
完整项目可在以下网址获得:https://github.com/pauldmps/BPUTApp-AndroidStudio如果有人想看一下整个代码。
最佳答案
您在 list 中指定了错误的主题:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
你应该使用 android:theme="@style/AppTheme"
。
关于android - ?attr/不在工具栏中设置正确的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28992683/