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

Android 使用 selector 動態切換 TextView 文字色與背景色的方法

編輯:關於 Android 程式設計

本文將為大家講解如何借助 selector 來動態修改 TextView 的文字顏色與背景顏色,滿足了控件在不同狀態下呈現不同外觀的需求,希望能給大家帶來啟發。

selector 的妙用

說到 selector,Android 開發者應該都不陌生。它能讓控件在不同動作狀態下輕鬆實現顏色等數值的變化。而我們知道,Button 在源碼層面其實就是一種特殊的 TextView,所以很多時候我們直接用 TextView 加上一句 android:clickable="true" 就能替代按鈕。

在 TextView 上使用 selector 時,通常會遇到兩種情況:一種是直接在 TextView 上判斷按下、焦點等動作;另一種則是由 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"/>

二、由外層控件把狀態傳回 TextView

這種情況也很常見:我們通常會在外層套一個 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 程序設計有所幫助。

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