无法在 android 可绘制对象 xml 中添加属性原色
Unable to add attribute primary color in android drawable xml
我将主题定义为
<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
</style>
在我的 XML 布局中,我正在做
<android.support.design.widget.AppBarLayout
android:background="?attr/colorPrimary"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
每当我更改任何主题时,colorPrimary 都会更改
但是,如果我在可绘制对象中添加了相同的东西,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
它因无法膨胀视图而崩溃,视图的背景设置为 @drawabe/xxxx
如何在我的 XML 可绘制对象中定义主题颜色属性
这似乎在 API 21+ 上受支持。在此之下,您不能引用 drawable 中的属性。
https://code.google.com/p/android/issues/detail?id=26251.
这可能有助于解决问题:
How to reference style attributes from a drawable?
只需替换...
android:color="?attr/colorPrimary"
到...
android:color="@color/colorPrimaryRed"
更新
无法在 API 21 以下的 xml 可绘制对象中引用属性。
为了制作您的主题,您需要:
为每个主题创建一个 xml 可绘制对象。
直接使用@color 标签或#RGB 格式将所需的颜色包含到您的可绘制对象中。
在 attrs.xml 中为您的可绘制对象创建一个属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="my_drawable" format="reference" />
</resources>
将您的可绘制对象添加到您的 theme.xml。
<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
<item name="my_drawable">@drawable/my_drawable</item>
</style>
使用您的属性在您的布局中引用您的可绘制对象。
<TextView android:background="?my_drawable" />
你能试试吗
<solid android:color="@android:attr/colorPrimary"/>
而不是
<solid android:color="?attr/colorPrimary" />
您可以使用:-
<solid android:color="@android:color/holo_orange_dark" />
我将主题定义为
<style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimaryRed</item>
<item name="colorPrimaryDark">@color/colorPrimaryDarkRed</item>
<item name="colorAccent">@color/colorAccentRed</item>
</style>
在我的 XML 布局中,我正在做
<android.support.design.widget.AppBarLayout
android:background="?attr/colorPrimary"
android:id="@+id/topBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
每当我更改任何主题时,colorPrimary 都会更改
但是,如果我在可绘制对象中添加了相同的东西,例如
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorPrimary" />
<corners android:radius="5dp"/>
</shape>
</item>
</selector>
它因无法膨胀视图而崩溃,视图的背景设置为 @drawabe/xxxx
如何在我的 XML 可绘制对象中定义主题颜色属性
这似乎在 API 21+ 上受支持。在此之下,您不能引用 drawable 中的属性。 https://code.google.com/p/android/issues/detail?id=26251.
这可能有助于解决问题: How to reference style attributes from a drawable?
只需替换...
android:color="?attr/colorPrimary"
到...
android:color="@color/colorPrimaryRed"
更新
无法在 API 21 以下的 xml 可绘制对象中引用属性。 为了制作您的主题,您需要:
为每个主题创建一个 xml 可绘制对象。 直接使用@color 标签或#RGB 格式将所需的颜色包含到您的可绘制对象中。
在 attrs.xml 中为您的可绘制对象创建一个属性。
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- Attributes must be lowercase as we want to use them for drawables -->
<attr name="my_drawable" format="reference" />
</resources>
将您的可绘制对象添加到您的 theme.xml。
<style name="MyTheme" parent="@android:style/Theme.NoTitleBar">
<item name="my_drawable">@drawable/my_drawable</item>
</style>
使用您的属性在您的布局中引用您的可绘制对象。
<TextView android:background="?my_drawable" />
你能试试吗
<solid android:color="@android:attr/colorPrimary"/>
而不是
<solid android:color="?attr/colorPrimary" />
您可以使用:-
<solid android:color="@android:color/holo_orange_dark" />