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

5
我有一个形状(rect_shape.xml),它在列表视图(listview_style.xml)的每个项目中绘制描边轮廓线。这个轮廓线应该具有与当前主题默认文本颜色相同的颜色。
有没有任何方法,在XML中将描边的android:color值设置为当前文本颜色?
我看到了一些类似的问题(比如像如何在我的样式中获取自定义属性值),他们试图设置自己的属性,但我不认为这是我想要的。不管怎样,我试过了,但我无法将android:color值设置为我自己定义的属性(android:color="?custom_stroke_color"会抛出InflateException)。
由于用户能够动态地在不同的主题之间切换,所以在rect_shape.xml中使用单个预定义颜色(或对颜色资源的引用,例如@color/white)是不可行的。
感谢您的帮助... 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

在 Android 5.0 及以上版本中,才能访问 drawable XML 文件中的主题属性。

请参见 此问题


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接