我在我的项目中使用了http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html中的相同代码。
当我创建一个空白项目以仅测试工具栏时,该颜色可以正常工作。但是,在使用相同的代码将项目升级到材料设计后,工具栏颜色变为灰色。
似乎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>
&styles.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。
您在清单中指定了错误的主题:
<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"
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句