Android使用selector修改TextView中字體顏色和背景色的方法
本文實(shí)例講述了Android使用selector修改TextView中字體顏色和背景色的方法。分享給大家供大家參考,具體如下:
android中的selector大家都很熟悉了,用它可以很方便的實(shí)現(xiàn),控件在不同的動(dòng)作中,顏色等值的變化。這里我說一下TextView中的一些應(yīng)用。
我想大家都知道,Button按鈕在源碼上看是一種特殊的TextView,所以我們很多時(shí)候,按鈕全是使用的TextView來完成,只要加一個(gè)android:clickable="true"就可以了。
TextView在使用selector時(shí),會(huì)有兩種情況,一種就是正常的在TextView控件上來判斷按下,焦點(diǎn)等動(dòng)作的判斷,另一種則是TextView外層控件的這些動(dòng)作,然后將動(dòng)作傳回TextView.
一,正常的在TextView控件上來判斷按下,焦點(diǎn)等動(dòng)作的判斷
這種相對(duì)簡(jiǎn)單,一般要修改控件的背景色和文字色,我們要用到兩個(gè)xml文件。代碼如下:
tbackground.xml 修改背景
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 默認(rèn)時(shí)的背景圖片--> <!--<item android:drawable="@color/white" />--> <!-- 沒有焦點(diǎn)時(shí)的背景圖片 --> <item android:state_window_focused="false" android:drawable="@color/white" /> <item android:state_focused="false" android:state_pressed="true" android:drawable="@color/btnbackBlue" /> </selector>
這里要說明一點(diǎn),大家看到了,我把默認(rèn)時(shí)的背景圖片(顏色)給注了,這是為什么呢,因?yàn)槟惆堰@條放在最前面,無論什么時(shí)候,它都會(huì)最先運(yùn)行,它運(yùn)行完了,程序就不會(huì)再往下運(yùn)行了,所以下面寫的全都沒有了。如果你想設(shè)置默認(rèn)值,請(qǐng)把這行代碼,放到最下面。
ttextcolor.xml 修改文字
<?xml version="1.0" encoding="utf-8" ?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 沒有焦點(diǎn)時(shí)的背景圖片 --> <item android:state_window_focused="false" android:color="@color/black" /> <item android:state_focused="false" android:state_pressed="true" android:color="@color/white" /> <!-- 默認(rèn)時(shí)的背景圖片--> <item android:color="@color/black" /> </selector>
文字的修改我就把默認(rèn)值,放到了最后,這里也要說一下,背景我們要用android:drawable而文字的顏色要使用android:color,不然會(huì)報(bào)錯(cuò),為什么?大家想想。哈哈。。。。
<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外層控件的這些動(dòng)作,然后將動(dòng)作傳回TextView.
這種情況也常出現(xiàn),我們一般會(huì)在外層加一個(gè)LinearLayout或是RelativeLayout。而我們會(huì)把點(diǎn)擊的事件,給這個(gè)外層控件。這時(shí)候,你要修改的就是外層控件的背景,和TextView控件的文字顏色。這個(gè)時(shí)候,我們還用上面的方式,你會(huì)發(fā)現(xiàn),TextView沒有反應(yīng),為什么,因?yàn)樗鼪]有得到事件,這個(gè)時(shí)候,會(huì)用到一個(gè)屬性就是android:duplicateParentState
它的官方解釋是”如果設(shè)置此屬性,將直接從父容器中獲取繪圖狀態(tài)(光標(biāo),按下等)。 注意僅僅是獲取繪圖狀態(tài),而沒有獲取事件,也就是你點(diǎn)一下LinearLayout時(shí)Button有被點(diǎn)擊的效果,但是不執(zhí)行點(diǎn)擊事件“??聪旅娴拇a:
<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>
我們?cè)谛薷耐鈱涌丶尘暗耐瑫r(shí),也在修改 TextView文字的顏色.
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android TextView字體顏色設(shè)置方法小結(jié)
- Android中EditText和AutoCompleteTextView設(shè)置文字選中顏色方法
- Android編程實(shí)現(xiàn)TextView字體顏色設(shè)置的方法小結(jié)
- Android編程設(shè)置TextView顏色setTextColor用法實(shí)例
- Android TextView設(shè)置不同的顏色字體
- Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
- Android編程實(shí)現(xiàn)TextView部分顏色變動(dòng)的方法
- Android Textview實(shí)現(xiàn)顏色漸變滾動(dòng)效果
- Android 實(shí)現(xiàn)不同字體顏色的TextView實(shí)現(xiàn)代碼
- Android中TextView實(shí)現(xiàn)分段顯示不同顏色的字符串
相關(guān)文章
android判斷應(yīng)用是否已經(jīng)啟動(dòng)的實(shí)例
這篇文章主要介紹了android判斷應(yīng)用是否已經(jīng)啟動(dòng)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03android handler.post和handler.sendMessage的區(qū)別和聯(lián)系
handler.post和handler.sendMessage本質(zhì)上是沒有區(qū)別的,都是發(fā)送一個(gè)消息到消息隊(duì)列中,而且消息隊(duì)列和handler都是依賴于同一個(gè)線程的。接下來通過本文給大家分享android handler.post和handler.sendMessage的區(qū)別和聯(lián)系,一起看看吧2017-08-08Android實(shí)現(xiàn)二級(jí)購物車的全選加反選、總價(jià)功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)二級(jí)購物車的全選加反選、總價(jià)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04Android系統(tǒng)實(shí)現(xiàn)DroidPlugin插件機(jī)制
這篇文章主要為大家詳細(xì)介紹了Android系統(tǒng)上實(shí)現(xiàn)DroidPlugin插件機(jī)制,可以在無需安裝、修改的情況下運(yùn)行APK文件,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-01-01Android SDK Manager解決更新時(shí)的問題 :Failed to fetch URL...
本文主要介紹解決安裝使用SDK Manager更新時(shí)的問題:Failed to fetch URL...,這里提供了詳細(xì)的資料及解決問題辦法,有需要的小伙伴可以參考下2016-09-09使用Android Studio創(chuàng)建OpenCV4.1.0 項(xiàng)目的步驟
這篇文章主要介紹了使用Android Studio創(chuàng)建OpenCV4.1.0 項(xiàng)目的步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10詳解Android的MVVM框架 - 數(shù)據(jù)綁定
這篇文章主要介紹了詳解Android的MVVM框架 - 數(shù)據(jù)綁定,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05