【发布时间】: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