java – 无法在android drawable xml中添加属性主色
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 无法在android drawable xml中添加属性主色,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1794字,纯文字阅读大概需要3分钟。
内容图文
![java – 无法在android drawable xml中添加属性主色](/upload/InfoBanner/zyjiaocheng/789/d2dab4202a814ac2b5e7062b53a52cee.jpg)
我把我的主题定义为
<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中添加属性主色所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。