在我的应用程序中,用户可以在蓝色和绿色之间切换主题。在 android 5.0 以上版本中一切正常,但在 android 5.0 以下我的应用程序崩溃。
下面是我的风格代码
<style name="AppTheme.Blue" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/blue</item>
<item name="colorPrimaryDark">@color/bllueDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.Green" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/bluish_green</item>
<item name="colorPrimaryDark">@color/bluish_green</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
我的drawable文件代码是
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="?colorPrimary"></solid>
<corners android:radius="3dp"></corners>
</shape>
</item>
我的布局是
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="@drawable/my_drawable">
//other code
</androidx.constraintlayout.widget.ConstraintLayout>
最佳答案
较低 Android API 级别的 XML 可绘制对象不支持 ?attr/
引用。我假设它在引入时是 21 级,但我找不到任何有关它的文档。
因此您必须直接使用颜色资源。如果您需要为动态主题使用不同的颜色,您也许可以使用主题提供的正确颜色的可绘制对象来实现类似的效果。
或者,您可以在 UI 甚至支持方面忽略这些较旧的 Android 版本。
关于java - ?ColorPrimary 在 android 5.0 以下不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54707275/