如何使用textColorPrimary作为样式中的背景颜色?

joh*_*nes 9 android styles

我想创建一个使用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>
Run Code Online (Sandbox Code Playgroud)

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

ant*_*nyt 6

在尝试使用属性时,此语法似乎对我有用:

<TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="?android:textColorPrimary"
        android:text="Hello"/>
Run Code Online (Sandbox Code Playgroud)

(要么)

<style name="MyStyle">
   <item name="android:textColor">?android:textColorPrimary</item>
</style>
Run Code Online (Sandbox Code Playgroud)

我可以将应用主题从Holo更改为Holo.Light,文本颜色将自动更改以适应.

当我将它设置为View的背景时它不起作用 - Android会崩溃抱怨引用的drawable是一个没有指定drawables的状态列表(它是一个颜色状态列表).

    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
Run Code Online (Sandbox Code Playgroud)

我正在使用HoloEverywhere,它允许我直接引用资源,但你应该在本机情况下遇到类似的问题.我不认为在状态列表xml中用作组件的主要,未选择,未激活(等)颜色是通过属性公开的.

在任何情况下,使用的文本颜色取决于您(应用程序开发人员)选择的主题.如果您选择使用Holo(深色)主题,那么您的文字将是浅色,用户将无法对此产生影响.您无需为应用创建线条颜色动态.

  • 那么,如何使用它作为背景色? (2认同)

M-W*_*eEh 0

我认为您想使用原生 Android 主要文本颜色。

<item name="android:background">@android:color/primary_text_dark</item>
Run Code Online (Sandbox Code Playgroud)