Android筆記之TextView、Button、Imageiew點擊selector


一、Textiew動態改變顏色

概述:

  • 使用selector為Textiew設置各個狀態下的顏色
  • 在代碼中改變Textiew的顏色
  • 在代碼中setTextColor后如何還原selector中對Textiew的設定

1、使用selector為Textiew設置各個狀態下的顏色

(1)在res/color文件夾下新建title_color.xml文件

<item android:color="#0099cc"/>表示文本一般狀態下的顏色
pressed,focused,selected分別表示:按下,控件獲得焦點
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true" android:color="#FF0000"/>
    <item android:state_focused="true" android:color="#FF0000"/>
    <item android:state_pressed="true" android:color="#FF0000"/>
    <item android:color="#0099cc"/>
</selector>

(2)layout文件下的TextView:

<TextView
        android:id="@+id/hello"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:textColor="@color/title_color" />

(3)最后,這一步必不可少

    private TextView tv1;
...
tv1=(TextView)findViewById(R.id.hello); tv1.setOnClickListener(null);

 

 

也可以給textview的背景設置選擇器,而不是textview本身

 

    <TextView
        android:id="@+id/notice"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/setting_item_selector"
        android:padding="15dp"
        android:text="消息通知" />

 

2、在代碼中給Textiew設置顏色

(1)android自帶的顏色:tv1.setTextColor(Color.BLUE);

(2)/ res/ values/ colors.xml中定義的顏色:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="myGreen">#669966</color>
</resources>

代碼:

tv1.setTextColor(getResources().getColor(R.color.title_color));

xml中設置:

android:color="@color/myGreen"

 

(3)設置在/ res/ color/ title_color.xml下定義的顏色選擇器

tv1.setTextColor(getResources().getColorStateList(R.color.title_color));

 

 

 

 二、Button動態改變樣式

1、 Button的三種狀態:(pressed, focused, or niether)

2、在Button里為android:textColor設置顏色選擇器,可以動態Button上的文本顏色,使用和Textiew改變顏色基本相同

3、設置Button更美觀的樣式,並動態變化

(1)4張圖片

button.9.png     button_disable.9.png    button_down.9.png     button_highlighted.9.png  

(2)新建selector:preview_button_bg.xml

<?xml version="1.0" encoding="utf-8"?>
<selector
  xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_enabled="true" android:drawable="@drawable/preview_button_highlighted" />
    <item android:state_enabled="true" android:state_selected="true" android:drawable="@drawable/preview_button_highlighted" />
    <item android:state_enabled="true" android:state_pressed="true" android:drawable="@drawable/preview_button_highlighted" />
    <item android:state_enabled="false" android:drawable="@drawable/preview_button_disable" />
    <item android:drawable="@drawable/preview_button" />
</selector>

(3) 為Button設置屬性:

   android:background="@drawable/preview_button_bg"

 

三、Imageiew的selector

<ImageView
        android:id="@+id/imageview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/tab_home_btn">
    </ImageView>

selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>
    <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>
    <item android:drawable="@drawable/icon_home_nor"/>
</selector>

 

 

Done!

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM