2014-10-22 31 views
3

sdk更新到Android 5.0後消失了方法TextView.getTextColor(Context context, TypedArray typedArray, int defStyle)
我對此自定義TextView使用了此方法(用於從xml定義的int colorId)。
那麼如何從xml中確定int color id新的Android 5.0中消失了TextView.getTextColor(Context context,TypedArray typedArray,int defStyle)方法嗎?

+0

是公開的嗎? – Blackbelt 2014-10-22 08:43:30

+0

@blackbelt是) – 2014-10-22 08:51:03

+2

對於任何好奇的人,這個方法被刪除,因爲android.R.styleable既不公開也不穩定。因此,將TypedArray傳遞到任何框架視圖是不安全的,因爲數組索引不會與內部使用的android.R.styleable數組相匹配。這會以難以調試的方式破壞您的應用程序,因此我們刪除了所有這些方法。 – alanv 2014-10-22 23:47:57

回答

1

已確實刪除,你可以在API DIFF看到: https://developer.android.com/sdk/api_diff/21/changes.html

,您仍然可以使用這個變體:

public final ColorStateList getTextColors()

+0

你能給我關於TextView的具體參考https://developer.android.com/sdk/api_diff/21/changes.html嗎? – 2014-10-22 08:53:06

+0

@ xoxol_89 https://developer.android.com/sdk/api_diff/21/changes/android.widget.TextView.html – SLee 2014-10-22 08:56:20

+0

@ xoxol_89我已更新鏈接。 – Natix 2014-10-22 08:59:42

2

這裏是獲取顏色TextView的一個示例代碼:

TextView tv = (TextView) findViewById(R.id.yourComponentId); 
int tv_color = tv.getTextColors().getDefaultColor(); 

或者你也可以得到像這樣的正常文本的顏色:

TextView tv = (TextView) findViewById(R.id.yourComponentId); 
int tv_color = tv.getCurrentTextColor(); 

在使用的第一個例子的情況下,也可以使用

TextView tv = (TextView) findViewById(R.id.yourComponentId); 
ColorStateList colorStateList = tv.getTextColors(); 
int tv_color colorStateList.getColorForState(states, failColor); 

希望這有助於獲得各種狀態的顏色。

參考:getColorForState

+0

什麼是DefaultColor?如果開發人員輸入了具體的int color值,我該怎麼做呢? – 2014-10-22 09:03:10

+0

@ xoxol_89它返回具體的int顏色,就像Color類中定義的一樣。 – SLee 2014-10-22 09:38:21

+0

@Xoxol_89 TextView類的getCurrentTextColor方法返回相同的值。 – SLee 2014-10-22 09:43:38