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)文章
Ubuntu中為Android系統(tǒng)實(shí)現(xiàn)內(nèi)置Java應(yīng)用程序測(cè)試Application Frameworks層的硬件服務(wù)
本文主要介紹Ubuntu中為Android系統(tǒng)內(nèi)置應(yīng)用訪問Application Frameworks層的硬件服務(wù),這里提供了詳細(xì)的流程和代碼實(shí)例,有興趣的朋友可以參考下2016-08-08
android動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語(yǔ)言的方法
下面小編就為大家?guī)硪黄猘ndroid動(dòng)態(tài)設(shè)置app當(dāng)前運(yùn)行語(yǔ)言的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android圖片選擇器ImageEditContainer
這篇文章主要為大家詳細(xì)介紹了Android圖片選擇器ImageEditContainer的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Android Presentation實(shí)現(xiàn)雙屏異顯
這篇文章主要為大家詳細(xì)介紹了Android Presentation實(shí)現(xiàn)雙屏異顯,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-01-01
Android音視頻之視頻采集(系統(tǒng)API預(yù)覽)
這篇文章主要為大家詳細(xì)介紹了Android音視頻之視頻采集,系統(tǒng)API預(yù)覽,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

