java– 无法在android drawable xml中添加属性主色

java– 无法在android drawable xml中添加属性主色,第1张

概述我把我的主题定义为<stylename="AppThemeRed"parent="Theme.AppCompat.Light.NoActionBar"><!--Customizeyourthemehere.--><itemname="colorPrimary">@color/colorPrimaryRed</item><

我把我的主题定义为

<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都会发生变化

但是,如果我在drawable中添加相同的东西,例如

<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 drawable中定义主题颜色属性

解决方法:

只需要替换……

androID:color="?attr/colorPrimary"

至…

androID:color="@color/colorPrimaryRed"

更新

在API 21下面的xml drawable中引用属性是不可能的.
为了使您的主题成为您需要:

每个主题创建一个xml drawable.
使用@color标签或#RGB格式直接包含所需的颜色.

在attrs.xml中为drawable创建一个属性.

<?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>

将drawable添加到theme.xml中.

<style name="Mytheme" parent="@androID:style/theme.NoTitlebar">   <item name="my_drawable">@drawable/my_drawable</item></style>

使用您的属性在布局中引用您的drawable.

<TextVIEw androID:background="?my_drawable" />
总结

以上是内存溢出为你收集整理的java – 无法在android drawable xml中添加属性主色全部内容,希望文章能够帮你解决java – 无法在android drawable xml中添加属性主色所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1119515.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存