編輯:關於 Android 程式設計
本文將為大家講解如何借助 selector 來動態修改 TextView 的文字顏色與背景顏色,滿足了控件在不同狀態下呈現不同外觀的需求,希望能給大家帶來啟發。
說到 selector,Android 開發者應該都不陌生。它能讓控件在不同動作狀態下輕鬆實現顏色等數值的變化。而我們知道,Button 在源碼層面其實就是一種特殊的 TextView,所以很多時候我們直接用 TextView 加上一句 android:clickable="true" 就能替代按鈕。
在 TextView 上使用 selector 時,通常會遇到兩種情況:一種是直接在 TextView 上判斷按下、焦點等動作;另一種則是由 TextView 的外層控件接收這些動作,再將狀態傳回給 TextView。
這種方式相對簡單,要修改控件的背景色與文字色,一般需要用到兩個 xml 文件。
先看修改背景的 tbackground.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 沒有焦點時的背景 -->
<item android:state_window_focused="false" android:drawable="@color/white" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@color/btnbackBlue" />
</selector>
這裡要特別提醒一點:你可能注意到我把「默認背景」那條註釋掉了。這是因為如果把它放在最前面,無論處於什麼狀態它都會最先命中,命中後程序就不再往下匹配了,於是下面寫的所有規則都將失效。所以如果你想設置默認值,請把這一行放到最下面。
再看修改文字的 ttextcolor.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="https://schemas.android.com/apk/res/android">
<!-- 沒有焦點時 -->
<item android:state_window_focused="false" android:color="@color/black" />
<item android:state_focused="false" android:state_pressed="true" android:color="@color/white" />
<!-- 默認顏色放在最後 -->
<item android:color="@color/black" />
</selector>
這裡我把默認值放到了最後。另外要注意,背景要用 android:drawable,而文字顏色要用 android:color,否則會報錯——仔細想想其中的道理就不難理解了。
接著在布局中引用這兩個文件:
<TextView
android:id="@+id/txt_collection_cancel"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="取消"
android:textColor="@drawable/ttextcolor"
android:gravity="center"
android:background="@drawable/tbackground"
android:clickable="true"/>
這種情況也很常見:我們通常會在外層套一個 LinearLayout 或 RelativeLayout,並把點擊事件綁定在外層控件上。此時要修改的是外層控件的背景,以及 TextView 的文字顏色。如果沿用上面的做法,你會發現 TextView 毫無反應——因為它根本沒有拿到事件。這時候就需要用到 android:duplicateParentState 這個屬性。
它的官方解釋是:「如果設置此屬性,將直接從父容器中獲取繪圖狀態(光標、按下等)。注意僅僅是獲取繪圖狀態,而沒有獲取事件,也就是你點一下 LinearLayout 時 Button 有被點擊的效果,但是不執行點擊事件。」看下面的代碼:
<RelativeLayout
android:id="@+id/rela_collection_add"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="@drawable/tbackground"
android:clickable="true">
<View
android:id="@+id/line_collection_add"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/gray"
android:layout_gravity="center_vertical"
android:layout_alignParentBottom="true" />
<TextView
android:id="@+id/txt_collection_add"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="新建收藏夾"
android:textColor="@drawable/ttextcolor"
android:textSize="@dimen/ActionBar_title_size"
android:duplicateParentState="true"
android:gravity="center"
android:layout_above="@+id/line_collection_add" />
</RelativeLayout>
這樣一來,在切換外層控件背景的同時,也能同步改變 TextView 的文字顏色了。
希望本文所述對大家進行 Android 程序設計有所幫助。
Android ListView 分頁加載數據實戰:滑到底自動載入更多
熟悉 Android 的朋友都知道,不管是微博客戶端還是新聞客戶端,都離不開列表元件。可以說 ListView 是 Android 在資料展現方面最重要的元件之一。今天我
Hello,Android:從零開始寫出第一個安卓程式
專案緣起因為要參加某個資訊安全比賽,我選了「安卓 APK 行為分析與評估」這個課題,所以第一步得先搞懂安卓程式是怎麼撰寫和運行的。我們的第一個任務,就是寫出一個多人通訊的
限制 EditText 的輸入字符:禁止中文、只允許字母數字的幾種做法
只允許 EditText 輸入數字或 email 格式字符,官方文檔裡有現成方案,非常簡單。要限制成自定義的數字字符集合也不難,setKeyListener(KeyLis
仿微信對話列表:滑動顯示刪除按鈕的完整實作
微信對話列表的左滑刪除做得很細膩:整列跟著手指移動,超過一半就自動吸附展開,不夠就彈回原位,同時只允許一列處於展開狀態。本文以 GitHub 上的 SwipeListVi