【问题标题】:TextColor defined in style being applied to TextView, but not Button?样式中定义的TextColor被应用于TextView,但不是Button?
【发布时间】:2010-12-16 16:03:12
【问题描述】:

我已经定义了一些样式资源,其中包括带有已定义 TextColor 的 TextAppearance。然后我将样式应用到一些 TextViews 和 Buttons。所有样式都通过 TextView 实现,但不是 Button。由于某种原因, textColor 属性没有显示出来。这是一个错误,还是我在 Button 的情况下遗漏了什么?

这里是样式定义:

<?xml version="1.0" encoding="UTF-8"?>
<resources>

    <style name="TestApp">      
    </style>

    <!-- Text Appearances -->
    <style name="TestApp.TextAppearance">
        <item name="android:typeface">sans</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">16px</item>       
        <item name="android:textColor">#6666FF</item>
    </style>

    <!-- Widget Styles -->
    <style name="TestApp.Widget">
        <item name="android:layout_margin">3sp</item>
    </style>

    <style name="TestApp.Widget.Label">
        <item name="android:textAppearance">@style/TestApp.TextAppearance</item>
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
    </style>

    <style name="TestApp.Widget.Switch">
        <item name="android:textAppearance">@style/TestApp.TextAppearance</item>
        <item name="android:layout_width">100px</item>
        <item name="android:layout_height">100px</item>
    </style>

</resources>

这是我尝试应用它们的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
<TextView
    style="@style/TestApp.Widget.Label"
    android:text="This is my label." />
<TextView
    style="@style/TestApp.Widget.Label"
    android:text="This is my disabled label."
    android:enabled="false" />
<Button
    style="@style/TestApp.Widget.Switch"
    android:text="This is my switch." />
<Button
    style="@style/TestApp.Widget.Switch"
    android:text="This is my disabled switch."
    android:enabled="false" />
</LinearLayout>

【问题讨论】:

    标签: android


    【解决方案1】:

    显然实现这一点的方法是覆盖按钮样式中的textColor 属性。 Android 在其全局主题样式中采用这种方式:
    https://android.googlesource.com/platform/frameworks/base/+/master/core/res/res/values/styles.xml

    我很想知道为什么会这样。

    【讨论】:

    • 我遇到了确切的问题,我通过覆盖 textColor 解决了它。似乎按钮忽略了 textAppearance 属性。
    【解决方案2】:

    对于按钮的情况,通过属性定义文本颜色有两种方式:textColor和通过textAppearance定义的样式。

    textColor 设置的值(默认样式设置)将覆盖textAppearance 样式设置的任何文本颜色值。因此,您必须通过以下两种方式之一将textColor 属性设置为@null

    • 将样式中的textColor设置为@null:

      <style name="Application.Button">   
        <item name="android:textAppearance">@style/Application.Text.Medium.White</item>
        <item name="android:textColor">@null</item> 
      </style>
      
      <Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          style="@style/Application.Button" />
      
    • 在 Button XML 中将 textColor 设置为 @null:

      <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@style/Application.Button"   
      android:textColor="@null" />
      

    【讨论】:

      【解决方案3】:

      似乎还有一个 textAppearanceButton:

      http://developer.android.com/reference/android/R.attr.html#textAppearanceButton

      您可能想尝试一下。

      【讨论】:

      • 我不知道该属性。但是,它没有任何影响。由于某种原因,当通过样式提供时,Button 和派生类似乎忽略了 textColor 属性。只有在没有样式的情况下才会生效。
      猜你喜欢
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2016-07-02
      • 2017-12-13
      • 1970-01-01
      • 2014-08-23
      • 2012-02-04
      相关资源
      最近更新 更多