Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

踩坑記錄:Android TextView 禁用狀態下的文字顏色之謎

編輯:關於 Android 程式設計

今天項目裡遇到一個小細節:本來運行得好好的視覺效果,如今卻不對勁了。老實說這個問題應該已經存在一段時間,只是過去一直沒人去留意這麼不起眼的地方。恰好今天改代碼時撞上了它,於是索性刨根問底,順手把解決方案記錄下來。

還原整個過程

我們先寫一個類似的例子來復現整個過程,看看到底是什麼在作怪。

布局

先準備一個 TextView 的布局:

 

這裡只有一個 TextView,文字直接寫死,textColor 設為 white,而 white 的定義就是 #ffffff。

運行效果

隨後在項目裡跑起來。這個 TextView 掛了一個點擊事件,點擊後會響應一些邏輯;但在某個特定時機,它會變成 disable 狀態、無法點擊,此時的處理是:

 textView.setEnabled(false); textView.setText("不能操作");

也就是把它設為 disable,順便換上一段文案(這裡同樣寫死,純粹是為了方便觀察效果)。

坑爹的事情就在這裡發生了:運行之後,文字竟然從白色變成了黑色。可就奇怪了,我根本沒設過 disable 狀態的顏色,那抹黑色究竟從哪兒冒出來的?

於是重新建了個 demo,把上面這套邏輯完整復現了一遍,結果這次顯示卻是正確的?簡直讓人摸不著頭腦。翻來覆去看了看源代碼,又覺得沒什麼問題;再懷疑是不是兩邊的主題不一致,統一之後問題照舊。兩邊代碼幾乎一樣,效果卻天差地別。

後來想著試試不同主題會不會影響文字顏色,於是改了第一個工程的主題,disable 顏色果然變了——從白色變成了灰色。由此可以斷定:主題確實會影響 TextView 在 disable 狀態下的顏色。可它究竟是怎麼影響的?翻遍了整個主題裡關於 TextView 的屬性,卻沒找到 disable 狀態的顏色設置。再去改第二個工程的主題顏色,它依舊紋絲不動。這下真是無語了。

此路不通,只好回到第一步,把兩個工程放在一起對比。除了代碼不同,還有什麼差別?仔細一查,果然有——兩邊用的兼容包版本不一致:出問題的那個用的是 compile 'com.android.support:appcompat-v7:23.0.0',而正常的那個用的是 compile 'com.android.support:appcompat-v7:23.1.1'。既然如此,問題大概率就出在兼容包不同版本的處理差異上。

代碼探究

下面我們分別看看不同版本的兼容包裡到底做了什麼。

23.0.0

兼容包裡,TextView 最終都會走到 AppCompatTextView,先看它的構造:

 public class AppCompatTextView extends TextView { private AppCompatTextHelper mTextHelper; public AppCompatTextView(Context context) { this(context, null); } public AppCompatTextView(Context context, AttributeSet attrs) { this(context, attrs, android.R.attr.textViewStyle); } public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTextHelper = new AppCompatTextHelper(this); mTextHelper.loadFromAttributes(attrs, defStyleAttr); } @Override public void setTextAppearance(Context context, int resId) { super.setTextAppearance(context, resId); if (mTextHelper != null) { mTextHelper.onSetTextAppearance(context, resId); } } }

可以看到它內部創建了一個 AppCompatTextHelper,並由 mTextHelper 對 TextView 進行加工,核心邏輯在 loadFromAttributes():

 void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { Context context = mView.getContext(); TypedArray a = context.obtainStyledAttributes(attrs, VIEW_ATTRS, defStyleAttr, 0); final int ap = a.getResourceId(0, -1); a.recycle(); if (ap != -1) { TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance); if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) { setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false)); } appearance.recycle(); } a = context.obtainStyledAttributes(attrs, TEXT_APPEARANCE_ATTRS, defStyleAttr, 0); if (a.hasValue(0)) { setAllCaps(a.getBoolean(0, false)); } a.recycle(); final ColorStateList textColors = mView.getTextColors(); if (textColors != null && !textColors.isStateful()) { final int disabledTextColor; if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { disabledTextColor = ThemeUtils.getDisabledThemeAttrColor(context, android.R.attr.textColorSecondary); } else { disabledTextColor = ThemeUtils.getThemeAttrColor(context, android.R.attr.textColorSecondary); } mView.setTextColor(ThemeUtils.createDisabledStateList( textColors.getDefaultColor(), disabledTextColor)); } }

它先取出 TextView 的 ColorStateList(mView.getTextColors()),當這個值不為空且 isStateful 為 false 時,就會進入判斷。那 isStateful 又是怎麼算的?

 public boolean isStateful() { return mStateSpecs.length > 1; }

可見只有 mStateSpecs.length 大於 1 時才為 true。而我們這裡只設了一個顏色值,length 為 1,於是判定為 false,順理成章進了判斷分支。關鍵就在於:系統會先取一個 disable 的 TextColor,默認讀的是 textColorSecondary 屬性——所以只要你的 Theme 裡設了這個值,最終 disable 時顯示的就是它;接著系統把默認顏色與這個 disable 顏色重新組合成一個 ColorStateList 再設回 TextView。這也就是為什麼它一 disable 就變黑,哪怕你根本沒設過該值也照樣變黑。的確夠坑爹的,這也解釋了項目裡變黑的根因。

23.1.1

再來看看 23.1.1 的構造函數:

 public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mTintManager = TintManager.get(getContext()); mBackgroundTintHelper = new AppCompatBackgroundHelper(this, mTintManager); mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); mTextHelper = AppCompatTextHelper.create(this); mTextHelper.loadFromAttributes(attrs, defStyleAttr); mTextHelper.applyCompoundDrawablesTints(); }

同樣由 AppCompatTextHelper 處理部分屬性,邏輯在 loadFromAttributes():

 void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { final Context context = mView.getContext(); final TintManager tintManager = TintManager.get(context); TypedArray a = context.obtainStyledAttributes(attrs, VIEW_ATTRS, defStyleAttr, 0); final int ap = a.getResourceId(0, -1); if (a.hasValue(1)) { mDrawableLeftTint = createTintInfo(context, tintManager, a.getResourceId(1, 0)); } if (a.hasValue(2)) { mDrawableTopTint = createTintInfo(context, tintManager, a.getResourceId(2, 0)); } if (a.hasValue(3)) { mDrawableRightTint = createTintInfo(context, tintManager, a.getResourceId(3, 0)); } if (a.hasValue(4)) { mDrawableBottomTint = createTintInfo(context, tintManager, a.getResourceId(4, 0)); } a.recycle(); if (ap != -1) { TypedArray appearance = context.obtainStyledAttributes(ap, R.styleable.TextAppearance); if (appearance.hasValue(R.styleable.TextAppearance_textAllCaps)) { setAllCaps(appearance.getBoolean(R.styleable.TextAppearance_textAllCaps, false)); } appearance.recycle(); } a = context.obtainStyledAttributes(attrs, TEXT_APPEARANCE_ATTRS, defStyleAttr, 0); if (a.getBoolean(0, false)) { setAllCaps(true); } a.recycle(); }

由此可見,這一版根本沒有再給 TextView 添加 disable 的 TextColor——估計是 Google 官方也意識到「默認添加顏色」的做法不太合理,於是後續版本把它改掉了。這裡只處理了文本是否全大寫,以及為了配合 Material Design 給四周圖片上色。這也正是第二個 demo 表現正常的原因。

24.0.0

再看 24.0.0:

 public AppCompatTextView(Context context, AttributeSet attrs, int defStyleAttr) { super(TintContextWrapper.wrap(context), attrs, defStyleAttr); this.mDrawableManager = AppCompatDrawableManager.get(); this.mBackgroundTintHelper = new AppCompatBackgroundHelper(this, this.mDrawableManager); this.mBackgroundTintHelper.loadFromAttributes(attrs, defStyleAttr); this.mTextHelper = AppCompatTextHelper.create(this); this.mTextHelper.loadFromAttributes(attrs, defStyleAttr); this.mTextHelper.applyCompoundDrawablesTints(); }

套路一致,同樣走 loadFromAttributes():

 void loadFromAttributes(AttributeSet attrs, int defStyleAttr) { final Context context = mView.getContext(); final AppCompatDrawableManager drawableManager = AppCompatDrawableManager.get(); TintTypedArray a = TintTypedArray.obtainStyledAttributes(context, attrs, VIEW_ATTRS, defStyleAttr, 0); final int ap = a.getResourceId(0, -1); if (a.hasValue(1)) { mDrawableLeftTint = createTintInfo(context, drawableManager, a.getResourceId(1, 0)); } if (a.hasValue(2)) { mDrawableTopTint = createTintInfo(context, drawableManager, a.getResourceId(2, 0)); } if (a.hasValue(3)) { mDrawableRightTint = createTintInfo(context, drawableManager, a.getResourceId(3, 0)); } if (a.hasValue(4)) { mDrawableBottomTint = createTintInfo(context, drawableManager, a.getResourceId(4, 0)); } a.recycle(); final boolean hasPwdTm = mView.getTransformationMethod() instanceof PasswordTransformationMethod; boolean allCaps = false; boolean allCapsSet = false; ColorStateList textColor = null; if (ap != -1) { a = TintTypedArray.obtainStyledAttributes(context, ap, R.styleable.TextAppearance); if (!hasPwdTm && a.hasValue(R.styleable.TextAppearance_textAllCaps)) { allCapsSet = true; allCaps = a.getBoolean(R.styleable.TextAppearance_textAllCaps, false); } if (Build.VERSION.SDK_INT < 23 && a.hasValue(R.styleable.TextAppearance_android_textColor)) { textColor = a.getColorStateList(R.styleable.TextAppearance_android_textColor); } a.recycle(); } a = TintTypedArray.obtainStyledAttributes(context, attrs, R.styleable.TextAppearance, defStyleAttr, 0); if (!hasPwdTm && a.hasValue(R.styleable.TextAppearance_textAllCaps)) { allCapsSet = true; allCaps = a.getBoolean(R.styleable.TextAppearance_textAllCaps, false); } if (Build.VERSION.SDK_INT < 23 && a.hasValue(R.styleable.TextAppearance_android_textColor)) { textColor = a.getColorStateList(R.styleable.TextAppearance_android_textColor); } a.recycle(); if (textColor != null) { mView.setTextColor(textColor); } if (!hasPwdTm && allCapsSet) { setAllCaps(allCaps); } }

同樣沒有默認設置 disable 顏色,只處理了大寫等情況。所以這個版本也不會出問題。

總結

問題排查清楚了,解決辦法有兩個:一是調整對應支持包的版本,二是給 TextView 顯式設置一個 selector 顏色值。按理說,若一個文本需要 disable 狀態,本來就應該把 disable 顏色一併配好;但有時候視覺設計上並不要求顏色變化,只是單純不能點擊,於是便偷了懶沒設。歸根結底,養成良好的編程習慣才是最要緊的。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved