【问题标题】:Failed to set textColor in Android on some devices在某些设备上无法在 Android 中设置 textColor
【发布时间】:2019-07-24 06:26:57
【问题描述】:

我有一个游戏,我需要根据用户的输入来改变文字的颜色

我使用不同的方法在 2 个地方更改了文本,这两种方法在某些设备上都失败了,并且 textColor 在这些设备上总是变成黑色并带有白色笔划,但是我从未在任何地方的代码上添加笔划,到目前为止报告的设备是三星 Galaxy prime , 小米 Mi A2 Lite, 华为 P20 Pro, 但是我在我朋友的三星 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);
            }
    }
}

【问题讨论】:

  • 请区分你得到的和你想要的输出,最好使用一些图像
  • 我相信你说的是你分享的屏幕右上角?。如果不是,请提供清晰的图片说明问题出在哪里。
  • 添加了更清晰的图片

标签: android textcolor


【解决方案1】:

在终于接触到有此问题的设备后 我发现它在设备设置>辅助功能>高对比度文本

中打开了选项

如果用户打开此选项,它会强制所有 TextView 的 textColor 为黑色或白色

因此,如果用户启用或不启用此选项,您可以签入代码并使用以下代码采取相应措施

/**
 * 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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-10
    • 2015-06-18
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2014-10-17
    • 1970-01-01
    • 2014-10-27
    相关资源
    最近更新 更多