2013-02-04 88 views
-1

例如,如果背景爲白色,則文字顏色將爲黑色。如果BG是黑色的,文本將是白色的。藍BG,黃文等 更新:如何在TextView中設置文本的自動翻轉顏色?

// 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.BLACKColorInvertor.invert(Color.BLACK) == Color.WHITE

+0

抱歉,這是一個程序員論壇,所以你不可能在這裏得到應用程序使用的答案。請嘗試[超級用戶論壇](http://superuser.com/),其中提供了有關應用程序和操作系統使用的幫助。 –

+0

你有試過什麼嗎? – RobinHood

+0

會給更多的代碼請 –

回答

2

讓你的顏色的RGB值和255他們。減去:

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

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

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

如果要使用十六進制值看How to get RGB value from hexadecimal color code in java

+0

謝謝!你幫我。 –

-1

有可能只需使用簡單的條件

首先

1.Get顏色

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);

然後把一個條件,只要你想:

如果(tv1colorID == R.color.green){

tv2.setBackgroundColor(color.WHITE); //當你選擇顏色

}

+0

不可能測試所有可能的顏色。 –

+0

如何,所有的顏色都有它的十六進制代碼,你可以在條件下使用十六進制代碼 –

相關問題