【问题标题】:android:textColor no longer works in Marshmallowandroid:textColor 不再适用于棉花糖
【发布时间】:2016-03-20 16:53:32
【问题描述】:

我编写了一个依赖于资源中定义的颜色的应用程序。有些是直接在布局 XML 文件中设置的,有些是在代码中设置的。例子:

res/values/styles.xml中的颜色定义:

<color name="orvGyro">#33B5E5</color>

布局:

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dotSpace"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/orvGyro" />

代码中的颜色:

accStatus.setTextColor(getResources().getColor(R.color.somecolor));

该应用以 API 17 为目标。直到 Lollipop,它都能完美运行,显示正确的颜色。迁移到 Marshmallow (Cyanogenmod 13) 后,所有这些颜色都显示为橙色。在 Java 代码中而不是在资源中定义的其他颜色似乎可以正确显示。

我尝试将目标 API 更改为 23 并为 API 21+ 添加样式,但无济于事。

这里有什么问题?这是 CyanogenMod13 中的错误,还是我做错了什么?

编辑: 似乎这与从资源中获取颜色无关。如下所示对颜色进行硬编码也会给我橙色文本:

<TextView
    android:id="@+id/textView9"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dotSpace"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="#669900" />

编辑 2: 刚刚遇到 Android M Developer Preview - TextView android:textColor being ignored。这可以解释我正在经历的行为吗?

编辑 3: 当我动态生成内容而不是使用布局时,颜色会正确显示。示例:

    TextView newType = new TextView(rilLteCells.getContext());
    newType.setLayoutParams(new TableRow.LayoutParams(0, LayoutParams.WRAP_CONTENT, 2));
    newType.setTextAppearance(rilLteCells.getContext(), android.R.style.TextAppearance_Medium);
    newType.setTextColor(rilLteCells.getContext().getResources().getColor(getColorFromGeneration(cell.getGeneration())));
    newType.setText(rilLteCells.getContext().getResources().getString(R.string.smallDot));
    row.addView(newType);

【问题讨论】:

    标签: android android-styles android-6.0-marshmallow cyanogenmod


    【解决方案1】:

    使用ContextCompat 类,它是一个辅助类,用于以向后兼容的方式访问 API 级别 4 之后引入的上下文中的功能。

    accStatus.setTextColor(ContextCompat.getColor(context, R.color.somecolor));
    

    public static final int getColor (Context context, int id)

         返回与特定资源 ID 关联的颜色

    【讨论】:

    • 我会如何处理我直接在布局中设置的颜色?
    • 它将从您的颜色资源中获取适当的颜色。
    • 这正是行不通的。即使我在布局中指定了颜色资源,它也会显示为橙色。
    【解决方案2】:

    知道了。

    无论我在哪里遇到此问题,控件中显示的文本都是一个正方形 (U+2b1b)。当我更改此文本时(例如通过附加 X),只有正方形会显示为橙色,而字符串的其余部分具有所需的颜色。

    把它改成一个小方块 (U+25fc) 固定的东西。其他一些特殊字符会给我其他颜色 - 显然某些字符在 Marshmallow 上固定为某些颜色,而在早期版本中,它们可以像任何其他文本一样设置样式。

    【讨论】:

      【解决方案3】:

      在我的 Sony Xperia(Android 6.0 Marshmallow)上遇到了同样的问题。原因是启用了设置/辅助功能/高对比度文本(实验性)。

      当我禁用它时,它又按预期正常工作了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-01-18
        • 2016-08-27
        • 2016-03-15
        • 2019-11-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多