欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用selector修改TextView中字體顏色和背景色的方法

 更新時(shí)間:2016年01月13日 14:19:34   作者:chenguang79  
這篇文章主要介紹了Android使用selector修改TextView中字體顏色和背景色的方法,實(shí)例分析了selector方法的相關(guān)使用技巧,需要的朋友可以參考下

本文實(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ì)有所幫助。

相關(guān)文章

最新評(píng)論