在某些设备上无法在 Android 中设置 textColor
Failed to set textColor in Android on some devices
我有一个游戏,我需要根据用户输入更改文本的颜色
我使用不同的方法更改了 2 个地方的文本,这两种方法在某些设备上都失败了,并且 textColor 在这些设备上总是变成黑色和白色笔划,但是我从来没有在我的代码中的任何地方添加笔划,到目前为止报告的设备是三星galaxy prime,小米 Mi A2 Lite,华为 P20 Pro,但是我在我的一个朋友的 Samsung galaxy prime 上试过这个游戏并且运行完美,所以我无法在我的测试设备上产生这个问题
显示问题的图片,所有文本均为黑色,描边颜色为白色
普通图片
键盘部分的第一种方法我的代码如下
btns[t].setTextColor(ContextCompat.getColor(getContext(),R.color.text_color));
当用户设置夜间模式时,我将其更改为
btns[t].setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_night));
颜色是 text_color = #000000 / text_color_night = #ffffff
棋盘部分的第二种方法,我的代码是
private final Paint selectedBox = new Paint();
private final TextPaint letterTextNormal = new TextPaint();
private final TextPaint letterTextCorrect = new TextPaint();
private final TextPaint letterTextWrong = new TextPaint();
public PlayboardRenderer(Context con) {
letterTextNormal.setTextAlign(Align.CENTER);
letterTextNormal.setAntiAlias(true);
letterTextNormal.setTypeface(((MainActivity) con).getSelectedFont());
letterTextCorrect.setTextAlign(Align.CENTER);
letterTextCorrect.setAntiAlias(true);
letterTextCorrect.setTypeface(((MainActivity) con).getSelectedFont());
letterTextWrong.setTextAlign(Align.CENTER);
letterTextWrong.setAntiAlias(true);
letterTextWrong.setTypeface(((MainActivity) con).getSelectedFont());
}
private void drawBox(Canvas canvas, int x, int y, int row, int col, float scale, Box box, Word currentWord) {
int boxSize = (int) (BOX_SIZE * scale);
// scale paints
float textSize = (boxSize) - (boxSize / 5);
letterTextNormal.setTextSize(textSize);
letterTextCorrect.setTextSize(textSize);
letterTextWrong.setTextSize(textSize);
if (!box.isBlank() && !box.isEmpty())
if (box.getState() == Box.STATE.NORMAL) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextNormal);
} else if (box.getState() == Box.STATE.WRONG) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextWrong);
} else if (box.getState() == Box.STATE.CORRECT) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextCorrect);
}
}
}
在我终于拿到有这个问题的设备之后
我发现它已在设备 设置>辅助功能>高对比度文本
中打开选项
如果用户打开此选项,它会强制所有 TextView 的文本颜色为黑色或白色
因此,如果用户启用或不启用此选项,您可以签入代码并使用以下代码采取相应措施
/**
* Returns if the high text contrast in the system is enabled.
* <p>
* <strong>Note:</strong> You need to query this only if you application is
* doing its own rendering and does not rely on the platform rendering pipeline.
* </p>
*
* @return True if high text contrast is enabled, false otherwise.
*
* @hide
*/
public boolean isHighTextContrastEnabled() {
synchronized (mLock) {
IAccessibilityManager service = getServiceLocked();
if (service == null) {
return false;
}
return mIsHighTextContrastEnabled;
}
}
所以在你的代码中,你可以通过这样做来访问这个方法
AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();
我有一个游戏,我需要根据用户输入更改文本的颜色
我使用不同的方法更改了 2 个地方的文本,这两种方法在某些设备上都失败了,并且 textColor 在这些设备上总是变成黑色和白色笔划,但是我从来没有在我的代码中的任何地方添加笔划,到目前为止报告的设备是三星galaxy prime,小米 Mi A2 Lite,华为 P20 Pro,但是我在我的一个朋友的 Samsung galaxy prime 上试过这个游戏并且运行完美,所以我无法在我的测试设备上产生这个问题
显示问题的图片,所有文本均为黑色,描边颜色为白色
普通图片
键盘部分的第一种方法我的代码如下
btns[t].setTextColor(ContextCompat.getColor(getContext(),R.color.text_color));
当用户设置夜间模式时,我将其更改为
btns[t].setTextColor(ContextCompat.getColor(getContext(), R.color.text_color_night));
颜色是 text_color = #000000 / text_color_night = #ffffff
棋盘部分的第二种方法,我的代码是
private final Paint selectedBox = new Paint();
private final TextPaint letterTextNormal = new TextPaint();
private final TextPaint letterTextCorrect = new TextPaint();
private final TextPaint letterTextWrong = new TextPaint();
public PlayboardRenderer(Context con) {
letterTextNormal.setTextAlign(Align.CENTER);
letterTextNormal.setAntiAlias(true);
letterTextNormal.setTypeface(((MainActivity) con).getSelectedFont());
letterTextCorrect.setTextAlign(Align.CENTER);
letterTextCorrect.setAntiAlias(true);
letterTextCorrect.setTypeface(((MainActivity) con).getSelectedFont());
letterTextWrong.setTextAlign(Align.CENTER);
letterTextWrong.setAntiAlias(true);
letterTextWrong.setTypeface(((MainActivity) con).getSelectedFont());
}
private void drawBox(Canvas canvas, int x, int y, int row, int col, float scale, Box box, Word currentWord) {
int boxSize = (int) (BOX_SIZE * scale);
// scale paints
float textSize = (boxSize) - (boxSize / 5);
letterTextNormal.setTextSize(textSize);
letterTextCorrect.setTextSize(textSize);
letterTextWrong.setTextSize(textSize);
if (!box.isBlank() && !box.isEmpty())
if (box.getState() == Box.STATE.NORMAL) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextNormal);
} else if (box.getState() == Box.STATE.WRONG) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextWrong);
} else if (box.getState() == Box.STATE.CORRECT) {
canvas.drawText(Character.toString(box.getChar()), x + (boxSize / 2), y + (int) (textSize * 0.9), letterTextCorrect);
}
}
}
在我终于拿到有这个问题的设备之后 我发现它已在设备 设置>辅助功能>高对比度文本
中打开选项如果用户打开此选项,它会强制所有 TextView 的文本颜色为黑色或白色
因此,如果用户启用或不启用此选项,您可以签入代码并使用以下代码采取相应措施
/**
* Returns if the high text contrast in the system is enabled.
* <p>
* <strong>Note:</strong> You need to query this only if you application is
* doing its own rendering and does not rely on the platform rendering pipeline.
* </p>
*
* @return True if high text contrast is enabled, false otherwise.
*
* @hide
*/
public boolean isHighTextContrastEnabled() {
synchronized (mLock) {
IAccessibilityManager service = getServiceLocked();
if (service == null) {
return false;
}
return mIsHighTextContrastEnabled;
}
}
所以在你的代码中,你可以通过这样做来访问这个方法
AccessibilityManager am = (AccessibilityManager) this.getSystemService(Context.ACCESSIBILITY_SERVICE);
boolean isHighTextContrastEnabled = am.isHighTextContrastEnabled();