问题
I have written an app which relies on colors defined in resources. Some are set directly in the layout XML file, others are set in code. Examples:
Color definition in res/values/styles.xml
:
<color name="orvGyro">#33B5E5</color>
Layout:
<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" />
Color in code:
accStatus.setTextColor(getResources().getColor(R.color.somecolor));
The app targets API 17. Up to Lollipop, this has worked flawlessly, displaying the right colors. After migrating to Marshmallow (Cyanogenmod 13), all these colors display as orange. Other colors, which are defined in Java code and not in resources, seem to display correctly.
I've tried changing the target API to 23 and adding styles for API 21+, to no avail.
What's wrong here? Is that a bug in CyanogenMod13, or am I doing something wrong?
EDIT: It seems it's not about getting the color from the resource. Hard-coding the colors as shown below also gives me orange text:
<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" />
EDIT 2: Just came across Android M Developer Preview - TextView android:textColor being ignored. Could this explain the behavior I'm experiencing?
EDIT 3: When I generate content dynamically instead of using layouts, colors display correctly. Example:
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:
Use ContextCompat class, It is helper class for accessing features in Context introduced after API level 4 in a backwards compatible fashion.
accStatus.setTextColor(ContextCompat.getColor(context, R.color.somecolor));
public static final int getColor (Context context, int id)
Returns a color associated with a particular resource ID
回答2:
Got it.
Wherever I had this issue, the text displayed in the control was a single square (U+2b1b). When I alter this text (e.g. by appending an X), only the square will display in orange and the rest of the string has the desired color.
Changing it to a small square (U+25fc) fixed things. Some other special characters would give me other colors – apparently certain characters are fixed to certain colors on Marshmallow, when on earlier versions they could be styled like any other text.
回答3:
Faced the same problem on my Sony Xperia (Android 6.0 Marshmallow). The reason was that the Settings/Accessibility/High contrast text (experimental) was enabled.
When I disabled it, it worked fine again as expected.
来源:https://stackoverflow.com/questions/36116633/androidtextcolor-no-longer-works-in-marshmallow