2

たとえば、背景が白の場合、テキストの色は黒になります。BG が黒の場合、テキストは白になります。青色の背景、黄色のテキストなど 更新:

// method in MyActivity class
void changeBackgroundColor(int newColor) {
    activityLayout.setBackgroundColor(newColor);
    int invertingColor = ColorInvertor.invert(newColor);
    someTextView.setTextColor(invertingColor);
}

を呼び出すとactivity.changeBackgroundColor(Color.WHITE)someTextViewテキストの色を黒、つまり などに変更する必要がありColorInvertor.invert(Color.WHITE) == Color.BLACKますColorInvertor.invert(Color.BLACK) == Color.WHITE

4

3 に答える 3

5

色の RGB 値を取得し、 255 から減算します。

yourColor = Color.rgb(0,130,20);

invertedRed = 255 - 0;
invertedGreen = 255 - 130;
invertedBlue = 255 - 20;

text.setTextColor(Color.rgb(invertedRed,invertedGreen,invertedBlue));

16 進数値を使用する場合は、Java で 16 進数カラー コードから RGB 値を取得する方法を参照してください。

于 2013-02-04T14:56:42.153 に答える
0

簡単な条件を使用するだけで可能です。

1.色を取得する

2.状態を確認する

3.色を設定する

色を取得するには:

TextView tv1;
tv1=(TextView)findViewById(R.id.tv1);
ColorDrawable tv1color = (ColorDrawable) tv1.getBackground();

Android 3.0 以降を使用している場合は、色のリソース ID を取得できます。

int tv1colorId = tv1color.getColor();

色を設定するには:

TextView tv2;
tv2=(TextView)findViewById(R.id.tv2);
tv2.setBackgroundColor(0xFF00FF00);

次に、必要に応じて条件を設定します。

if (tv1colorID == R.color.green) {
   tv2.setBackgroundColor(color.WHITE); // As your choice color
}
于 2013-02-04T13:01:34.113 に答える