如何获取(引用)XML中的当前主题默认文本颜色

时间:2012-04-27 09:52:15

标签: android resources themes

我有一个形状( rect_shape.xml ),它在列表视图的每个项目中绘制笔划轮廓( listview_style.xml )。 此轮廓应具有与当前主题的默认文本颜色相同的颜色。

有没有办法,在XML 中,将笔画的android:颜色值设置为当前文本颜色?

我在这里看到了一些类似的问题(比如How to get my own defined attribute value in my style)试图设置自己的属性,但我认为这不是我想要的。 无论如何我试过但我无法将android:color值设置为我自己定义的属性(android:color="?custom_stroke_color"抛出InflateException)。

因为用户能够动态地在主题之间切换,所以 rect_shape.xml 中的一个预定义颜色(或对颜色资源的引用,例如@color/white)不是一个选项。 / p>

感谢任何帮助...

rect_shape.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<stroke android:width="3dp"
<!-- should be the current default text color -->
    android:color="#FFF" />

<solid/> 
...
</shape>

listview_style.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/rect_shape" >
...
</LinearLayout>

的themes.xml

<resources>
<style name="DarkTheme" parent="@style/Theme.Sherlock">
<!-- default text color is white -->
...
</style>
<style name="LightTheme" parent="@style/Theme.Sherlock.Light">
<!-- default text color is black-->
...
</style>
</resources>

1 个答案:

答案 0 :(得分:0)

只能在Android 5.0及更高版本中访问可绘制XML文件中的主题属性。

请参阅This issue