【发布时间】:2018-03-07 14:31:12
【问题描述】:
我正在尝试为我的应用主题定义自定义颜色。 这是我的做法:
定义自定义属性:
<declare-styleable name="ApplicationStyle">
<attr name="colorWeekdaysBg" format="color"/>
</declare-styleable>
定义应用风格:
<style name="ApplicationStyle" parent="Theme.AppCompat.NoActionBar">
<item name="android:editTextStyle">@style/EditTextStyle</item>
<item name="colorPrimaryDark">@color/dark_blue</item>
<item name="colorPrimary">@color/dark_blue</item>
<item name="colorAccent">@color/dark_blue</item>
<item name="colorWeekdaysBg">@color/access_weekdays</item>
</style>
在清单中设置样式:
<application
android:name=".App"
android:icon="@drawable/icon_launcher"
android:label="@string/app_name"
android:theme="@style/ApplicationStyle">
在drawable xml中使用这个属性:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:right="-1dp">
<shape android:shape="rectangle">
<stroke
android:width="1dp"
android:color="?colorWeekdaysBg" />
<corners
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp" />
</shape>
</item>
</layer-list>
但由于某种原因,它不会将我的颜色应用于可绘制对象。它改为应用透明颜色。
更奇怪的是,如果我用?colorAccent(在Theme.AppCompat 中定义)替换我的?colorWeekdaysBg,那么它会应用正确的颜色。
所以最后的问题是:你知道为什么它不起作用,以及如何解决它吗?
【问题讨论】:
-
你在哪里设置 colorWeekdaysBg 的颜色?在 colors.xml 中名为“access_weekdays”的变量中?
-
@Antonio 完全正确
-
您是否尝试在可绘制的 xml 中使用“@color/access_weekdays”?让我们尝试找出应用失去颜色的地方。
-
是的,这就是我在决定将其移至主题之前的使用方式
标签: android android-drawable android-styles android-color