当前位置: 首页 > 知识库问答 >
问题:

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

卫弘图
2023-03-14

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

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

我在这里看到过一些类似的问题(比如如何以我的风格获得我自己定义的属性值),它们试图设置自己的属性,但我认为这不是我想要的。无论如何,我试过了,但是我无法将android:color值设置为我自己定义的属性(android:color=“?custom\u stroke\u color”抛出充气异常)。

由于用户能够在主题之间动态切换,矩形中的单个预定义颜色(或对颜色资源的引用,例如@color/white)。xml不是一个选项。

谢谢你的帮助。。。

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>

主题。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个答案

张鸿宝
2023-03-14

访问可绘制XML文件中的主题属性仅在Android5.0及以上版本中可用。

查看此问题

 类似资料: