ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

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

2019-06-27 21:00:44  阅读:200  来源: 互联网

标签:java android android-drawable android-styles


我把我的主题定义为

<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,android-drawable,android-styles
来源: https://codeday.me/bug/20190627/1308670.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有