1

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

颜色定义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.截至棒棒糖,这有完美地工作,显示正确的颜色。迁移到棉花糖(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); 

回答

1

得到相关的颜色。

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

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

1

使用ContextCompat类,它是一种用于在向后兼容的方式访问API级4之后引入在上下文特征辅助类。

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

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

       返回与特定资源ID

+1

如果我直接在布局中设置颜色,我该怎么做? – user149408

+0

它将从您的色彩资源中选取合适的颜色。 –

+1

这正是不起作用的。即使在布局中指定颜色资源时,它也会显示为橙色。 – user149408

0

面对我的索尼Xperia(Android 6.0棉花糖)相同的问题。原因是设置/辅助功能/高对比度文本(实验性)已启用。

当我禁用它时,它按预期再次正常工作。