【问题标题】:Using Holo-Light theme doesn't set the correct color to TextViews使用 Holo-Light 主题不会为 TextViews 设置正确的颜色
【发布时间】:2014-04-07 23:25:59
【问题描述】:

背景

我正在为我的应用程序“App Manager”添加“Holo”和“Holo Light”(也许还有深色操作栏)的主题选择器。现在我首先通过 XML 文件尝试它们。

问题

我正在使用 Google 的支持库并尝试主题,但无论我使用什么属性和主题,我的 listView 上的 textViews 的颜色都有错误的颜色。

listView 包含 2 个这样定义的 TextView:

<TextView
    android:id="@+id/appLabelTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toLeftOf="@+id/isSystemAppImageView"
    android:layout_toRightOf="@+id/appIconImageView"
    android:ellipsize="marquee"
    android:text="label"
    android:textAppearance="?android:attr/textAppearanceLarge"
    tools:ignore="HardcodedText" />

<TextView
    android:id="@+id/appDescriptionTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/appLabelTextView"
    android:layout_below="@+id/appLabelTextView"
    android:layout_toLeftOf="@+id/isSystemAppImageView"
    android:ellipsize="marquee"
    android:text="description"
    android:textAppearance="?android:attr/textAppearanceSmall"
    tools:ignore="HardcodedText" />

Theme.AppCompat.LightTheme.AppCompat

当我以编程方式完成所有操作以及使用 ActionBarSherlock 库时,也会发生同样的情况。

这是我使用的主题配置(这次尝试了 ActionBarSherlock,但普通支持库也是如此):

<style name="BaseTheme" parent="@style/Theme.Sherlock.Light"></style>

<style name="AppTheme" parent="@style/BaseTheme">
    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>
    <item name="android:windowBackground">@color/normal_window_background</item>
</style>

和清单:

<application
    android:name="com.lb.app_manager.utils.App"
    android:allowBackup="true"
    android:description="@string/app_description"
    android:hardwareAccelerated="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >

我在这里展示的活动没有任何代码或 XML 将主题设置为与上面写的不同。

我尝试过的

我已尝试检查示例,并没有发现 XML 和代码之间存在明显差异。他们工作得很好......

此外,出于某种原因,偏好活动显示得很好。

ActionBarShelock 库也存在同样的问题。

问题

怎么可能?应该怎么做才能解决它?

【问题讨论】:

  • 你试过没有android:textAppearance属性吗?
  • @shoerat 是的。我真的不知道它为什么会发生。我已经搜索了我能想到的任何东西:隐藏样式,以编程方式设置颜色,......真的不知道为什么会发生这种情况。目前,我已经为每个 textView 设置了颜色以处理这种奇怪的情况。还选择使用卡片-UI:play.google.com/store/apps/details?id=com.lb.app_manager
  • 好的。我刚刚检查了TextViews 以上,对 Galaxy Nexus(库存 Android 4.3)的 Theme.AppCompat.Lighthere is the screenshot 进行了零修改。那里没有问题,对吧?我想问题出在特定的设备/模拟器上。您正在测试哪种设备/模拟器?
  • @shoerat 正如我已经提到的,当我使用示例代码(甚至是“hello world”项目)时,它运行良好。我已经在具有 4.3 和 4.4 android 版本的模拟器以及我的设备(带有 4.3 的 Galaxy S3 I9300)上测试了我的应用程序。 :(
  • 嗯.. 在 Play 商店中检查了您的应用,我可以看到您使用的是 ABS 主题。您可能会混合 AppCompat 和 Sherlock 样式吗?

标签: android android-listview actionbarsherlock android-theme android-support-library


【解决方案1】:

我已经找到了这个问题的原因。

它与任何 XML 或设置样式无关。

这是因为适配器的 LayourInflater。我通过应用程序上下文而不是活动之一创建了它。

它还解释了为什么我对示例项目的测试工作正常,因为我检查了错误的东西......

【讨论】:

  • 这为我正在重构的旧应用程序挽救了生命。
【解决方案2】:

在 Holo light 主题和其他 Light 主题中,我们需要设置 Text color 才能使用它。

  <TextView
  android:id="@+id/appLabelTextView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignParentTop="true"
  android:layout_toLeftOf="@+id/isSystemAppImageView"
  android:layout_toRightOf="@+id/appIconImageView"
  android:ellipsize="marquee"
  android:textcolor="#000"
  android:text="label"
  android:textAppearance="?android:attr/textAppearanceLarge"
  android:textStyle="bold"
  tools:ignore="HardcodedText" />

  <TextView
  android:id="@+id/appDescriptionTextView"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:layout_alignLeft="@+id/appLabelTextView"
  android:layout_below="@+id/appLabelTextView"
  android:layout_toLeftOf="@+id/isSystemAppImageView"
  android:ellipsize="marquee"
  android:textcolor="#000"
  android:text="description"
  android:textAppearance="?android:attr/textAppearanceSmall"
  tools:ignore="HardcodedText" />

希望这会奏效。试试这个,让我知道结果。

【讨论】:

  • 这就是我最终所做的,但这不是解决方案,因为当我在示例项目甚至“hello world”项目中使用全息光主题时,我会更改文本颜色自动已经到了一个黑色的。这就是为什么我问怎么可能。 :(
  • 自动设备将为 Theme.Black 和 Theme.Light 采用黑色。这取决于应用程序主题。如果要更改文本颜色,则必须手动更改它。我做了上述修改,它的工作原理..我会再次检查并更新你..
  • 您不必手动设置它。只需尝试使用相同的 xml 文件创建一个“hello world”项目(使用 ABS 或支持库),您就会发现它工作得很好。此外,此解决方案对我不起作用,因为我还希望能够让用户选择要使用的主题。
【解决方案3】:

我尝试了您使用 Theme.AppCompat 和 Theme.AppCompat.Light 提供的示例,它们在 API 级别 10 和 18 上都按预期正常工作。由于您没有提供完整代码,只提供了 sn-ps,一定是您的应用程序中的某个位置存在额外的 XML 或代码位导致问题。

您需要检查几件事:

  • 从您的 AppTheme 定义中删除这些条目,以确保它们不会干扰并导致问题:

    <item name="android:listSeparatorTextViewStyle">@style/PreferenceListHeader</item>
    <item name="android:windowBackground">@color/normal_window_background</item>
    
  • 对于不同的 API 级别,您可能有不同的样式定义。检查 res/values-v11/styles.xml、res/values-v14/styles.xml 等的内容,确保它们没有干扰

【讨论】:

  • 已经尝试删除自定义条目。还尝试使用默认的 Holo-Light 主题而不是使用任何类型的库,因此第二个建议无济于事(反正我没有特殊版本的“值”文件夹)。我知道它在新项目上运行良好,因为我自己测试过它,但我仍然无法找出原因。其他样式是对话框主题(不用于有问题的活动)和 "PreferenceListHeader" 的配置。 “textAppearance”仅出现在我提到的内容中,并且设置文本颜色仅用于appWidgets而不是活动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-07
  • 2020-12-09
  • 2019-10-27
相关资源
最近更新 更多