如何在样式中将textColorPrimary用作背景颜色?

11

我想创建一个使用Android textColorPrimary 作为背景颜色的样式。 我尝试了以下代码,但没有成功,结果是我的布局根本没有显示。

<style name="horizontalLine">
    <item name="android:layout_width">fill_parent</item>
    <item name="android:layout_height">1dp</item>
    <item name="android:background">?android:attr/textColorPrimary</item>
</style>

我如何在样式中使用textColorPrimary作为背景色?


类似的问题已经在这里提出了 链接。希望对您有所帮助。 - Ashish
@Ashish:完全不是,另一个问卷想知道如何一般设置颜色,我想知道如何使用textColorPrimary作为背景颜色。 - johannes
@g00dy:根据你的方法,我必须稍后从Java源代码中设置颜色,但我的问题是如何在xml样式中实现它。 - johannes
还有一部分是关于 xml 的 :-) 请再看一下我的先前评论,告诉我它是否有效:<item android:color="@color/default_text_color" /> - g00dy
@Slartibartfast:如果用户选择在其Android设备上使用非暗色主题,primary_text_dark会发生什么? - johannes
显示剩余3条评论
4个回答

8
这种语法似乎适用于我,当尝试使用属性时:
<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="?android:textColorPrimary"
        android:text="Hello"/>

(或者)

<style name="MyStyle">
   <item name="android:textColor">?android:textColorPrimary</item>
</style>

我可以将应用程序主题从Holo更改为Holo.Light,文本颜色也会自动更改以适应。
但是,如果我将其设置为视图的背景,则无法正常工作 - Android会崩溃并抱怨引用的可绘制对象是一个未指定可绘制对象的状态列表(它是一个颜色状态列表)。
    Caused by: org.xmlpull.v1.XmlPullParserException: Binary XML file line #2: <item> tag requires a 'drawable' attribute or child tag defining a drawable
    at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:178)
    at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:885)
    at android.graphics.drawable.Drawable.createFromXml(Drawable.java:822)
    at android.content.res.Resources.loadDrawable(Resources.java:1950)
    ... 39 more

我正在使用HoloEverywhere,它让我可以直接引用资源,但在原生情况下你可能会遇到类似的问题。我认为在状态列表xml中使用的主要、非选定、非激活(等等)颜色作为组件的属性没有暴露出来。
无论如何,所使用的文本颜色取决于您作为应用程序开发人员选择的主题。如果您选择使用Holo(深色)主题,则您的文本将是浅色,并且用户无法影响此颜色。您不需要使您的行颜色对您的应用程序动态化。

2
那么,我该如何将其用作背景颜色? - johannes
@johannes,我在这里的回答中提供了几个选项,让你可以将其设置为样式或视图的背景颜色。 - Adil Hussain

3

要将?android:attr/textColorPrimary属性应用于视图的背景,您有两个选项。

第一个选项

第一种选择是定义一个shape可绘制对象,它从?android:attr/textColorPrimary属性获取其颜色,如下所示...

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="?android:attr/textColorPrimary" />
</shape>

假设您已将此可绘制资源命名为rectangle_shape_with_primary_text_color.xml并放置在应用程序的res/drawable文件夹中,您可以按照以下方式将其设置为样式的背景...

<item name="android:background">@drawable/rectangle_shape_with_primary_text_color</item>

...或将其直接设置为您视图的背景,如下所示...

android:background="@drawable/rectangle_shape_with_primary_text_color"

第二种选择

第二种选择是设置样式或视图的backgroundTint属性。

您可以按照以下方式设置样式的backgroundTint属性:

<style name="horizontalLine">
    <item name="android:background">@android:color/white</item>
    <item name="android:backgroundTint">?android:attr/textColorPrimary</item>
</style>

或者您可以直接设置视图的 backgroundTint 属性,如下所示:

<View
    android:id="@+id/horizontalLine"
    android:layout_width="match_parent"
    android:layout_height="1dp"
    android:background="@android:color/white"
    android:backgroundTint="?android:attr/textColorPrimary" />

请注意,使用这种方法时,background属性的确切值并不重要,但它必须被设置,并且不能被设置为@null@android:color/transparent

2
我得到的错误信息是:
org.xmlpull.v1.XmlPullParserException: Binary XML file line #18: <item> tag requires a 'drawable' attribute or child tag defining a drawable

经过一番调查,规范表明background属性应支持颜色或对可绘制资源的引用:

......查看您引用的资源,发现它是一个StateListDrawable

platforms/android-17/data/res/color/primary_text_dark.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:color="@android:color/bright_foreground_dark_disabled"/>
    <item android:state_window_focused="false" android:color="@android:color/bright_foreground_dark"/>
    <item android:state_pressed="true" android:color="@android:color/bright_foreground_dark_inverse"/>
    <item android:state_selected="true" android:color="@android:color/bright_foreground_dark_inverse"/>
    <item android:state_activated="true" android:color="@android:color/bright_foreground_dark_inverse"/>
    <item android:color="@android:color/bright_foreground_dark"/> <!-- not selected -->
</selector>

然而,StateListDrawable 的文档也明确表示 item 元素必须定义 drawable 属性:
https://developer.android.com/guide/topics/resources/drawable-resource.html

<item>
    Defines a drawable to use during certain states, as described by its attributes. Must be a child of a <selector> element.

    attributes:

    android:drawable
        Drawable resource. Required. Reference to a drawable resource.

这并不是primary_text_dark.xml所遇到的情况。因此,它无法正常工作,因为你引用的可绘制对象似乎不符合规范。

我认为解决方法是引用在默认状态下使用的颜色:bright_foreground_dark。由于它不是公开的,因此你需要直接访问它所引用的颜色:

android:background="@android:color/background_light"

0

我理解你想要使用原生安卓主要文本颜色。

<item name="android:background">@android:color/primary_text_dark</item>

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